In a script, I want to print what command is to be being executed.
Generally I use :
Now echo "COMMAND : $CMD" show this :
Which is correct; and should give CNT=3 which is correct.
But when I try to execute $CMD I get this error :
I have try
Which give also an error
How to execute $CMD with success.
Last edited by Scrutinizer; 05-27-2012 at 02:04 AM..
Reason: code tags
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.
I note that one of drl's examples includes this method already.
Seems to be not possible to use the same variable to print to screen and to execute when mixing single quote and double quote :
echo $CMD give :
I must separate the initial variable into 2 temporaries variables : Now echo $CMD give :
But to execute the command using eval I must use another temporary variable CMD_3:
The full script i_s now :
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:
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:
And you need to use double quotes around variable references:
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:
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)