简体   繁体   中英

execute printf command shell script

I am a beginner in writing shell script (probably my 1st shell script). I have a shell script that I like to run. The command is a simple grep command based on the parameter passed to it. I managed to get the correct grep command using the script below.

FILE='testurls'
MERCHANT_ID='8'
printf "grep '^%s' %s\n" "$MERCHANT_ID" "$FILE"

./hello.sh 
grep '^8' testurls

How would I execute the command instead of printing it.

printf is not necessary

FILE='testurls'
MERCHANT_ID='8'
grep "^$MERCHANT_ID" "$FILE"

You can use shell backticks quotes. This will store the output of executable commands.

Syntax are :-

1) Legacy Bourne shell backticks ``:

var=`grep "^$MERCHANT_ID" "$FILE"` 
printf "%s\n" "$var"

2) $() syntax:

var=$(grep "^$MERCHANT_ID" "$FILE")
printf "%s\n" "$var"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM