简体   繁体   English

执行printf命令shell脚本

[英]execute printf command shell script

I am a beginner in writing shell script (probably my 1st shell script). 我是编写shell脚本(可能是我的第一个shell脚本)的初学者。 I have a shell script that I like to run. 我有一个喜欢运行的shell脚本。 The command is a simple grep command based on the parameter passed to it. 该命令是基于传递给它的参数的简单grep命令。 I managed to get the correct grep command using the script below. 我设法使用下面的脚本来获取正确的grep命令。

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 不需要printf

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

You can use shell backticks quotes. 您可以使用Shell反引号。 This will store the output of executable commands. 这将存储可执行命令的输出。

Syntax are :- 语法是:-

1) Legacy Bourne shell backticks ``: 1)旧版Bourne贝壳反引号``:

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

2) $() syntax: 2)$()语法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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