Programming :: Using Variable To Store Command Lines In A Pipeline?
Jan 28, 2010
Was trying to write a shell script that has if conditional statements to decide different arguments for a command.Basically:
Code:
if [ "$1" = 1 ]
then
[code]...
View 6 Replies
ADVERTISEMENT
Jan 28, 2010
Code:
ls Again the command can be stored in a variable and then executed. Like
Code:
var=ls
&var
The above two codes are the same. The problem occurs when we try to pipeline it. Consider the following problem:
Code:
ls | grep *
works fine...but when we try to store it in a variable and run the command there is an error.
Code:
var="ls|grep *"
$var
how to store this kind of commands in a variable?
View 11 Replies
View Related
Apr 3, 2010
Suppose I want to account number of files beginning with abc , I can use "ls 'abc* | grep abc | wc -l", this will return me a number.
I want to store this number in a variable, say var1, so I tried
1. "ls 'abc* | grep abc | wc -l |read var1", but this didn't work as var1 has no value somehow.
2. var1='ls 'abc* | grep abc | wc -l', this just assign the entire string "ls 'abc* | grep abc | wc -l" to var1, which is not I wanted.
I don't want to store the value to a temporary file and then read the value from that file. I think there should be a direct way to get the value, but don't know how. I know in tcsh, one can just use set var1='ls 'abc* | grep abc | wc -l', but it also doesn't work in bash. Can anyone give any clue about this?
View 5 Replies
View Related
Apr 30, 2010
I have this script:
Code:
a=awk'{print $8}' f1.txt
How can I make it store lines in $a ? In the way above it creates one big line.
View 2 Replies
View Related
Jan 18, 2010
Suppose that there are two files
Code:
$ cat abc1
a1 a2
[code]...
View 4 Replies
View Related
Mar 3, 2010
I tried using the tail command in my shell script and storing that value in a variable a but an error keeps coming. Is there any other way to store the output of a command into a variable. Cannot Read text from text file and store it in a variable using shell script. The thing is I need a number from the file new.txt and use that number in my script
#!/bin/bash
a = `tail -1 new.txt|head -n 1`
echo $a
View 2 Replies
View Related
Apr 19, 2011
I want to store the result of wc -l as a variable so I can use it later in my script...so far unsuccessfully.
I have tried this:
set `echo awk '{ print $1, $6}' | wc -l` | echo $1
but it is far from working.
View 11 Replies
View Related
Jun 2, 2011
I am not able to execute a multiple sed statement using pipe filters in a variable. i am trying to extract a path from a file and then working on that path to change a few letters within the path by going through another sed statement.
the code looks like this code...
View 2 Replies
View Related
Apr 1, 2009
Want to return current LINES=, tryng to avoid the "hacky" backticks and string manipulation way
ie: my $somevar = `set | grep LINES`
I tried using the env command and it returns a empty value?
my $lines = $ENV{'LINES'};
$= doesnt work well for putty sessions...
my $lines = $=;
View 6 Replies
View Related
Apr 9, 2011
What is the effect of pipe symbol in the second statement shown below? How does it differ from first one?
set nt [open "file" "r"]
set nt [open "|file" "r"]
View 9 Replies
View Related
Jan 20, 2011
I need to find one RUNNING word in latest created logs.
and as soon as i will get RUNNING WORD , i have to execute another Unix Command.
I wrote following script code...
View 3 Replies
View Related
Nov 19, 2008
suppose i store the history number of a command say :
1004 cat file
Then now i want to run it like : !1004 but by using a variable.
command=1004
!$command
i am getting errors like :
command=1004command
View 2 Replies
View Related
Jun 7, 2010
I am searching for a file in a specific folder using the following command and storing it in a log file
find . -maxdepth 2 -type f -name "components.xml" -print | while read obj
do
basename `dirname $obj` >> avail_list.log
done
Here I want to do the following
1. Store each entry after processing in an array variable.
2. Use this array variables as arguments to function calls.
Here my primary objective is to store the entries from 'find' command in an array. So it is ok for me if I am not creating the log file.
View 5 Replies
View Related
May 2, 2010
i am on processing text tasks And i found that if you assign a text to a variable is chomp'ed automatically the newline
Code:
variable=$(cat file.txt)
The problem is i can only access the items/lines using:
Code:
for line in $variable
do
echo $line
# Other commands
done
how do i convert this to an indexed array. More importantly, how do i get access to individual $line[0], ..., $line[n] Another thing, if the file.txt, has lines with spaces it is a mess using the for...in..., but echoing prints line by line...o_0
View 11 Replies
View Related
Jul 23, 2010
Is there a command which can be used to run some other command on a few lines from a file or an o/p of some file. (the kind of role that -exec option does for the find command). (I have solved the purpose using a bash loop but would like to know if there exists a command).
View 4 Replies
View Related
Apr 14, 2011
i'm trying to execute a shell script, i'm trying to use the values in an array for use in a sed command:
sed -n '/Sales ID: ${array[$i]}/,/Totals:/p'
that command creates empty files. so my guess is that its not recognizing the array as an array but as text?
how would i be able to utilize the array in the command? i got it, didnt think that if i doubled up the single quotes that it would work, but this worked:
sed -n '/Sales ID: '${array[$i]'}/,/Totals:/p'
View 4 Replies
View Related
Aug 7, 2010
I want to display 4 options using the case command and refresh the screen when options 1 and 2 are chosen (no changes to the options and you get asked again to chose option), but give a message for option 3 and exit on option 4. I set this up with the script below, but choosing option 1 works and choosing option 2 exits the script.
Code:
#!/bin/bash
#testing options
Option="0"
[code]....
View 7 Replies
View Related
Dec 23, 2010
I have the following code :
Code:
E_BADARGS=65
if [ $# -ne 2 ] ; then
[code]...
View 1 Replies
View Related
Feb 16, 2011
I have following contents
I want to grep "#2" and want the output as
How to using shell script?
View 6 Replies
View Related
Mar 1, 2011
I am having all sorts of trouble trying to assign a variable within an awk script with the system command. I know there is a lot of ways around this problem, but for efficiency reasons, I would like to, within my awk script, do something like
system(x=3)
or
system(x=NR)
and, latter on the shell script which calls the awk script, use the variable $x. But nothing is passed to x. I have already tried things like
command = "x=3"
system(command)
and also used a pipeline within the system to pipe it to /bin/sh In fact tried a lot of stuff like that, using $(( )) etc etc etc I can create directories e write to files (yes, i could write to a file and read from there, but I dont think it is efficient, plus I am puzzled).
View 7 Replies
View Related
Feb 11, 2011
I would like to put a variable in the $PS1 prompt that will change each time a command is run. I want the color of the $PS1 prompt to change each time a command is run.I know that I can do this:
Code:
PS1="h@w # "
## "#" is changes every time a command is run
[code]....
View 9 Replies
View Related
May 4, 2010
I'm trying to create a program that would locate the oldest file of a certain type on a server. Here's the commands:
OLDEST_PATH=`find -L / -depth -maxdepth 6 -mindepth 6 -type d | sort -f | head -1`
OLDEST_FILE=`find -L $OLDEST_PATH | grep .mp3 | sort -f | head -1`
ls -al $OLDEST_FILE
I'm writing this all in expect but I'm having problems. The main problem I have is whenever I try to run the first command, I can't seem to isolate the result of the OLDEST_PATH so that the 2nd command will work. There always seems to be a newline in the variable and the result is only "find -L" command running and it bypasses the variable. If I can just figure out how to get the 1st and 2nd command to work, then I can figure out the 3rd. Here's some code:
Code:
send "find -L / -depth -maxdepth 6 -mindepth 6 -type d | sort -f | head -1
"
sleep 20
expect -re "(.*)
[Code]....
I know there is a better way to write this. I've tried multiple ways and this just happens to be the last way I've tried it. If you try running this, you'll notice that there is still carriage returns after the result of OLDEST_PATH and it prevents the 2nd "find" command from working properly.
View 2 Replies
View Related
Jun 15, 2010
I have the following TCL code:
if{ [catch {sqlite3 db /path/to/db/file} result] } {
puts stderr $result
} else {
do something
}
What I want is to use a variable for the file name/path. When I put in a variable instead of the absolute path, I get an error: "missing close-brace: possible unbalanced brace in comment while executing"
This is weird because when I run the code with the fully qualified pathname, it works fine. I substitute out the pathname for a variable containing the pathname ($variable) and I get this error.
View 1 Replies
View Related
Mar 5, 2010
I need to store the output of bitset() in a variable ... is it possible in c++?
View 1 Replies
View Related
Feb 12, 2011
I'm trying to execute the following command within a bash script:
Code:
tac /var/log/system.log | head -1
The script I wrote is:
[code]...
View 1 Replies
View Related
Jul 20, 2011
I have model output data in ascii format. It contains thousands of lines. The output file contains multiple text lines with variable values. here I copy-paste some of it's contents.
Code:
73438 170 23:53:20 3.481328E-03 1.824611E+04 1.824612E+04 1.333962E+16
73439 170 23:56:40 3.481210E-03 1.824611E+04 1.824612E+04 1.333962E+16
73440 171 00:00:00 3.481093E-03 1.824611E+04 1.824612E+04 1.333962E+16
[code]....
i want to remove the lines starting by WRT and DEF.
View 7 Replies
View Related
Nov 24, 2009
How do you remove parts of strings using python? Such as, if I have something like:
Code:
erme1 sdifskenklsd
erme2 sdfjksliel
[code]....
View 3 Replies
View Related
Aug 21, 2009
I'm trying to read content of file to variable and use this variable in for loop. The problem is, when I have c++ comment style in file - /*. Spaces in line are also interpreted as separated lines.
For example:
Code:
Changing $files to "$files" eliminate these problems but causes that whole content of variable is treated as one string (one execution of loop).
View 6 Replies
View Related
Apr 7, 2010
my script has a variable which comes in the form +00.00 +0.00 -00.00 or -0.00 (the numbers can be any in that form) for any that have a + symbol I need to remove the +, but if it has a - symbol it needs to stay.
i need to make a new variable with the string from the old variable btut without any plus sign. I have tried a lot of different ways with no success, each thing I tried either left the + or removed the entire string. I think this should work but doesn't
foo=+12.40
bar=${foo#+}
View 4 Replies
View Related
Apr 25, 2011
how I can search within a variable and assign the results to a new variable. I'll use the following as an example -
cars="Audi BMW Cadillac Chevy Dodge Ferrari Ford Mercedes"
list=`echo ${cars} | egrep -o '<A?+|<C+'`
with the echo command I get the following output assigned to list -
A
C
C
What I'd like to get for output is -
Audi
Cadillac
Chevy
how I could do this regardless of upper/lower case letters?
View 5 Replies
View Related