Operating System - HP-UX
1753378 Members
5022 Online
108792 Solutions
New Discussion юеВ

Issue with shell scripts environment variable

 
SOLVED
Go to solution
kaushikbr
Frequent Advisor

Issue with shell scripts environment variable

Hi All

I'm writing a script in which the command I need to run might change depending on certain conditions. Therefore I'm building the command line and saving this command in an environment variable, which I run at a later stage.

Something like RUN_CMD="ls -l | grep -v -E "total|SERVER" | cut -d' ' -f1|sort -r"

The command line is just an example, but I have to combine more than one commands using a pipe.

When I try to run this constructed command line, I get error messages like -v no such file etc.


|grep: unknown host name.
-v: unknown host name.
-E: unknown host name.
"HOST_NAME|host1|host2"|: unknown host name.
cut: unknown host name.
-d': unknown host name.
': unknown host name.
-f1|sort: unknown host name.
-r: unknown host name.

Thanks in advance for your suggetions.

Regards
Kaushik
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: Issue with shell scripts environment variable

Hi:

First, use either double or single quotes *only* to surround the whole command:

# RUN_CMD="ls -l | grep -v -E "total|SERVER" | cut -d' ' -f1|sort -r"

Now, do:

# eval ${RUN_CMD}

Regards!

...JRF...
kaushikbr
Frequent Advisor

Re: Issue with shell scripts environment variable

Just had to stick a eval at the beginning of the variable while running the command

eval ${RUN{COMMAD}

Kaushik
Dennis Handly
Acclaimed Contributor

Re: Issue with shell scripts environment variable

>JRF: use either double or single quotes *only* to surround the whole command:

I assume this was an exercise for the user?
This works:
RUN_CMD="ls -l | grep -v -E 'total|SERVER' | cut -d' ' -f1 | sort -r"
kaushikbr
Frequent Advisor

Re: Issue with shell scripts environment variable

Hi

This is what I have done in my script.

RUN_CMD="ls -l ${CLUSTERS} | ${PATH_GREP} -v -E \"HOST_NAME | ${SERVER_MANAGER}\" | cut -d' ' -f1 | sort -r"
.
.
.
.
eval $RUN_CMD > ${TMP_FILE}

This worked.

JRF's suggestion to -
use either double or single quotes *only* to surround the whole command:

also worked.

#RUN_CMD="ls -l server02/ | grep -v -E 'total|SERVER' | cut -d' ' -f1|sort -r"

#eval $RUN_CMD

Once again thank you all for your suggestions.

Thanks and Regards
K