[英]execute bash commands with options from a file
I am having hard time executing bash commands (with options) read from a text file. 我很难执行从文本文件中读取的bash命令(带有选项)。 In my script there's a for loop which reads the commands from the file but errors out: 在我的脚本中,有一个for循环,该循环从文件中读取命令,但出错:
cat com.txt
apt-get update
my script: 我的脚本:
for i in `cat com.txt` ; do sudo bash -c $i ; done;
The error msg displayed is: 显示的错误消息为:
apt 1.0.1ubuntu2 for amd64 compiled on Oct 28 2014 20:55:14
Usage: apt-get [options] command
apt-get [options] install|remove pkg1 [pkg2 ...]
apt-get [options] source pkg1 [pkg2 ...]
apt-get is a simple command line interface for downloading and
installing packages. The most frequently used commands are update
and install.
Commands:
update - Retrieve new lists of packages
upgrade - Perform an upgrade
install - Install new packages (pkg is libc6 not libc6.deb)
remove - Remove packages
autoremove - Remove automatically all unused packages
purge - Remove packages and config files
source - Download source archives
build-dep - Configure build-dependencies for source packages
dist-upgrade - Distribution upgrade, see apt-get(8)
dselect-upgrade - Follow dselect selections
clean - Erase downloaded archive files
autoclean - Erase old downloaded archive files
check - Verify that there are no broken dependencies
changelog - Download and display the changelog for the given package
download - Download the binary package into the current directory
Options:
-h This help text.
-q Loggable output - no progress indicator
-qq No output except for errors
-d Download only - do NOT install or unpack archives
-s No-act. Perform ordering simulation
-y Assume Yes to all queries and do not prompt
-f Attempt to correct a system with broken dependencies in place
-m Attempt to continue if archives are unlocatable
-u Show a list of upgraded packages as well
-b Build the source package after fetching it
-V Show verbose version numbers
-c=? Read this configuration file
-o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp
See the apt-get(8), sources.list(5) and apt.conf(5) manual
pages for more information and options.
This APT has Super Cow Powers.
bash: update: command not found
In your example, you're not executing the whole line. 在您的示例中,您没有执行整条线。 You just execute apt-get
(which expects an argument) and then update
(which is not a command). 您只需执行apt-get
(需要一个参数),然后update
(这不是命令)。
Just use source
of execute command inside a file with bash
(execution bit not needed): 只需在带有bash
的文件内使用execute命令的source
(不需要执行位):
source com.txt
This is one of the many reasons not to read lines with cat
in a for
loop . 这是不使用for
循环中的cat
读取行的众多原因之一。 Use a while read -r
instead. 使用while read -r
代替。
while read -r cmd; do
sudo $cmd
done <com.txt
From your example, the bash -c
seems superfluous, but this depends on your commands, and on what privileges you have set up in sudoers
. 从您的示例来看, bash -c
似乎是多余的,但这取决于您的命令以及在sudoers
设置的特权。 For anything more complex, you're likely to run into http://mywiki.wooledge.org/BashFAQ/050 and the source
answer will make more sense than the possible workarounds. 对于更复杂的事情,您可能会遇到http://mywiki.wooledge.org/BashFAQ/050 ,并且source
答案比可能的解决方法更有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.