有人可以请帮助,因为我似乎无法找到解决方案。我有以下脚本正常工作:使用BASH脚本变量执行命令
#!/bin/bash
#Checks the number of lines in the userdomains file
NUM=`awk 'END {print NR}' /etc/userdomains.hristian`;
echo $NUM
#Prints out a particular line from the file (should work with $NUM eventually)
USER=`sed -n 4p /etc/userdomains.hristian`
echo $USER
#Edits the output so that only the username is left
USER2=`echo $USER | awk '{print $NF}'`
echo $USER2
然而,当我与变量$ NUM线12代替4,这样,它不工作:
USER=`sed -n $NUMp /etc/userdomains.hristian`
我试着许多不同的引号和$ {}的组合,但是他们都不起作用,因为我是BASH的新手。请帮助:)
你没有尝试所有的组合:'USER = \'sed -n $ {NUM} p /etc/userdomains.hristian \''。作为一个方面说明:_不要在Bash中使用大写变量名!_在这里你做错了:'USER'很可能是一个变量集并被你的系统使用;你可能迟早会发生冲突!作为附加说明:不要使用反引号,而应使用'$(...)'代替:'USER = $(sed -n $ {NUM} p /etc/userdomains.hristian)'。 – 2015-02-07 14:27:24
你想打印文件的最后一行吗?如果是这样,'tail -n1/etc/userdomains.hristian'将更有效地完成这项工作。 – 2015-02-07 14:34:04
显然是最后一行的最后一个字段。最简单的方法是'awk'END {print $ NF}'/ etc/userdomains.hristian',尽管使用'tail -n1 /etc/userdomains.hristian | awk'{print $ NF}''对于大文件会更有效率。 – Wintermute 2015-02-07 14:37:43