Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi.
Here are a few methods:
Code:
#!/usr/bin/env bash
# @(#) s1 Demonstrate execution of created shell command.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C
pl " Create, display, attempt to execute shell command:"
cmd='count=$( wc -l < s1 )'
pe " Command is \"$cmd\""
$cmd
pl " Write command to file, use as input to shell:"
echo "$cmd" > f1
echo "echo \$count" >> f1
cat f1
bash f1
pe " Current value of count is \"$count\""
pl " Write command to file with here document, use as input to shell:"
cat <<EOF > f1
$cmd
echo "count is \$count"
EOF
cat f1
bash f1
pe " Current value of count is \"$count\""
pl " Create, display, attempt to execute shell command, using eval:"
unset count
echo " count before eval is \"$count\""
cmd='count=$( wc -l < s1 )'
pe " Command is \"$cmd\""
pe " Current value of count is \"$count\""
eval $cmd
echo " count after eval is \"$count\""
pl " Create, display, attempt to execute shell command, using source:"
unset count
echo " count before source is \"$count\""
cmd='count=$( wc -l < s1 )'
pe " Command is \"$cmd\""
echo "$cmd" > f1
pe " Current value of count is \"$count\""
source f1
echo " count after source is \"$count\""
exit 0
producing:
Code:
% ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0.8 (lenny)
bash GNU bash 3.2.39
-----
Create, display, attempt to execute shell command:
Command is "count=$( wc -l < s1 )"
./s1: line 14: count=$(: command not found
-----
Write command to file, use as input to shell:
count=$( wc -l < s1 )
echo $count
51
Current value of count is ""
-----
Write command to file with here document, use as input to shell:
count=$( wc -l < s1 )
echo "count is $count"
count is 51
Current value of count is ""
-----
Create, display, attempt to execute shell command, using eval:
count before eval is ""
Command is "count=$( wc -l < s1 )"
Current value of count is ""
count after eval is "51"
-----
Create, display, attempt to execute shell command, using source:
count before source is ""
Command is "count=$( wc -l < s1 )"
Current value of count is ""
count after source is "51"
The double quotes around "some simple command" are causing the command substitution contained therein to be executed. Use single quotes to get the actual literal command displayed. Then, use eval to force the shell to evaluate the line where you want to run the command twice. The first pass replaces "eval $CMD" with "CNT=$(who|grep -c username)" then it gets executed.
eval can be dangerous if a user can affect what is eval'd, but since you are controlling the command to be eval'd you should be ok here.
It is indeed possible, but you have to be aware about the different number of times the line gets evaluated with and without eval.
If you use eval var='$(...)' to execute it you should probably print it with the same number of evals. But to be honest i have a feeling that your whole script is not planned very well because the structures you use are looking wrong anyway. I may be wrong, but if you post your whole script and tell us what you want to achieve we could find a better way to do it than with such crooked measures.
If you try double quotes instead of single quotes, I think it becomes a bit more straight forward, you should then escape the double quotes with a backslash, plus the $-signs that you wish to protect from evaluation during assignment:
Code:
CMD="CNT=\$( grep -c -e \"Non-option program arguments: '$PACKAGE_1'\" -e \"Checking whether to refresh metadata for $REPO_1\" -e \"Selecting '$PACKAGE_1-3.5.0-7.x86_64' from repository '$REPO_1' for installation.\" \"$LOG_FILE_1\" )"
If you use a here-document you do not need the double quotes around the string and therefore you do not need to escape the double quotes inside:
Code:
read CMD << EOF
CNT=\$( grep -c -e "Non-option program arguments: '$PACKAGE_1'" -e "Checking whether to refresh metadata for $REPO_1" -e "Selecting '$PACKAGE_1-3.5.0-7.x86_64' from repository '$REPO_1' for installation." "$LOG_FILE_1" )
EOF
And you need to use double quotes around variable references:
Code:
printf "%s\n" "$CMD"
eval "$CMD"
A third option, since this is just a deferred command in a variable and there is no reason for evaluating one $sign earlier than the next you could use:
Code:
read CMD << "EOF"
CNT=$( grep -c -e "Non-option program arguments: '$PACKAGE_1'" -e "Checking whether to refresh metadata for $REPO_1" -e "Selecting '$PACKAGE_1-3.5.0-7.x86_64' from repository '$REPO_1' for installation." "$LOG_FILE_1" )
EOF
Then you do not need to escape any character, but then the variable names will show up when you print the content of $CMD, which you may or may not prefer...
This User Gave Thanks to Scrutinizer For This Post:
I have a below syntax its working fine...
var12=$(ps -ef | grep apache | awk '{print $2,$4}')
Im getting expected output as below:
printf "%b\n" "${VAR12}"
dell 123
dell 456
dell 457
Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Hi All,
I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Hi all,
Hereby wish to have your advise for below:
Main concept is
I intend to get current directory of my script file.
This script file will be copied to /etc/init.d.
A string in this copy will be replaced with current directory value.
Below is original script file:
... (6 Replies)
can anyone please help me with this:
i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure...
... (4 Replies)
Hi,
I am trying to do the following thing
var='date'
$var
Above command substitutes date for and in turn runs the date command and i am getting the todays date value.
I am trying to do the same thing as following, but facing some problems,
unique_host_pro="sed -e ' /#/d'... (3 Replies)
Dear all,
I have basic knowledge of Unix script and her I am trying to process variable length and variable format CSV file.
The file length will depend on the numbers of Earnings/Deductions/Direct Deposits.
And
The format will depend on whether it is Earnings/Deductions or Direct Deposits... (2 Replies)
Hi all,
I have a variable say var1 (output from somewhere, which I can't change)which store something like this:
echo $var1
name=fred
age=25
address="123 abc"
password=pass1234
how can I make the variable $name, $age, $address and $password contain the info?
I mean do this in a... (1 Reply)
I want to instert Category:XXXXX into the 2. line
something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong:
sed "2i\\${n}Category:$cat\n"
Sample:
Titel Blahh Blahh abllk sdhsd sjdhf
Blahh Blah Blahh
Blahh
Should look like... (2 Replies)
Hello,
i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem.
my var1 is a string constructed like this:
filename1 filerev1 filepath1
my var2 is another string constructed like this:
filename2 filerev2 filepath2
when i do... (2 Replies)