command in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting command in variable
# 1  
Old 07-22-2010
command in variable

I am making a pipeline of several commands.
This is a part of my shell script.
Code:
array=(
 "mkdir src"
 "ls -l | grep tk_ > src/list"
)
len=${#array[*]}

i=0;
while [ $i -lt $len ]; do
    echo ${array[$i]}
    if !(${array[$i]}); then exit 1; fi;
    let i++
done
---------------------------------------
"mkdir src" is working well but "ls -l | grep tk_ > src/list" does  not work.
These are error messages.
-----------------------------------------
ls: cannot access |: No such file or directory
ls: cannot access grep: No such file or directory
ls: cannot access tk_: No such file or directory
ls: cannot access >: No such file or directory
ls: cannot access src/list: No such file or directory

How can I solve this problem?

Thanks.
Image Image Image No tags yet, please tag this post - what is this?

Moderator's Comments:
Mod Comment You use "tags" in your signature more than in your post... so I added a couple to your post... we call them "code tags" Smilie

Last edited by Scott; 07-22-2010 at 05:14 PM.. Reason: Code tags
# 2  
Old 07-22-2010
One or two modifications later:

Code:
array=(
 "mkdir src"
 "ls -l | grep tk_ > src/list"
)
len=${#array[@]}

i=0;
while [ $i -lt $len ]; do
    echo "${array[$i]}"
    if [ -z "${array[$i]}" ]; then exit 1; fi;
    let i++
done

So now it echo's
Code:
mkdir src
ls -l | grep tk_ > src/list

Change echo to eval to run those.

That link you posted showed quality. According to that, you:

Quote:
Registered: Dec 1969
Posts: 0
Thanked: 0
# 3  
Old 07-22-2010
It works well. Thanks a lot.
# 4  
Old 07-22-2010
Quote:
Originally Posted by scottn
One or two modifications later:

Code:
array=(
 "mkdir src"
 "ls -l | grep tk_ > src/list"
)
len=${#array[@]}

i=0;
while [ $i -lt $len ]; do
    echo "${array[$i]}"
    if [ -z "${array[$i]}" ]; then exit 1; fi;
    let i++
done

So now it echo's
Code:
mkdir src
ls -l | grep tk_ > src/list

Change echo to eval to run those.

That's unnecessarily complex. Try:
Code:
array=(
 "mkdir src"
 "ls -l | grep tk_ > src/list"
)

for cmd in "${array[@]}"
do
  echo "$cmd"
done

This User Gave Thanks to cfajohnson For This Post:
# 5  
Old 07-22-2010
Quote:
Originally Posted by cfajohnson

That's unnecessarily complex. Try:
Code:
array=(
 "mkdir src"
 "ls -l | grep tk_ > src/list"
)

for cmd in "${array[@]}"
do
  echo "$cmd"
done

You're right Smilie

Rather than correcting the code, I should have looked to improving it. Nice one.

Last edited by Scott; 07-22-2010 at 08:20 PM.. Reason: Original wording was odd
# 6  
Old 07-22-2010
Thanks.
This is my new code.

Code:
array=(
 "mkdir src"
 "ls -l | grep tk_ > src/list"
)

for cmd in "${array[@]}"
do
  echo "$cmd"
  if !(eval $cmd); then exit 1; fi;
done

If a command fails to run, the whole process shoud stop.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

2. Shell Programming and Scripting

Command in a variable

i want to copy the command awk -F "=" '{print $2}' in a user defined variable for later/multiple use... Please let me know how to do that.. (11 Replies)
Discussion started by: shreedhar.rk123
11 Replies

3. Shell Programming and Scripting

SH script, variable built command fails, but works at command line

I am working with a sh script on a solaris 9 zone (sol 10 host) that grabs information to build the configuration command line. the variables Build64, SSLopt, CONFIGopt, and CC are populated in the script. the script includes CC=`which gcc` CONFIGopt=' --prefix=/ --exec-prefix=/usr... (8 Replies)
Discussion started by: oly_r
8 Replies

4. Shell Programming and Scripting

Variable to command to Variable Question KSH

Hello, First post for Newbie as I am stumped. I need to get certain elements for a specific PID from the ps command. I am attempting to pass the value for the PID I want to retrieve the information for as a variable. When the following is run without using a variable, setting a specific PID,... (3 Replies)
Discussion started by: Coyote270WSM
3 Replies

5. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

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)
Discussion started by: gvinayagam
3 Replies

6. Shell Programming and Scripting

How to use variable with command substitution in variable

For example I have variable like below echo $OUTPUT /some/path/`uname -n` when I try to use the variable OUTPUT like below cd $OUTPUT or cd ${OUTPUT} I am getting bad substituion error message $ cd $OUTPUT ksh: cd: bad substitution $ cd ${OUTPUT} ksh: cd: bad substitution ... (1 Reply)
Discussion started by: rajukv
1 Replies

7. UNIX for Dummies Questions & Answers

sed insert command and variable expansion/command substitution

I know this script is crummy, but I was just messing around.. how do I get sed's insert command to allow variable expansion to show the filename? #!/bin/bash filename=`echo $0` /usr/bin/sed '/#include/ { i\ the filename is `$filename` }' $1 exit 0 (8 Replies)
Discussion started by: glev2005
8 Replies

8. Shell Programming and Scripting

Using variable in ls command

I am in /home/aaa path and trying to list all the files in different directory - the path to this directory is lengthy. For example: aa/bb/cc/dd/ee/ff/gg/hh/ii/jj... and also the path will change dynamically. HEnce I capture the directory path in a variable. How to use this variable in ls... (10 Replies)
Discussion started by: Sangtha
10 Replies

9. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

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)
Discussion started by: lowmaster
2 Replies

10. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies
Login or Register to Ask a Question