我想写这样的法典:
command="some command"
safeRunCommand $command
safeRunCommand() {
cmnd=$1
$($cmnd)
if [ $? != 0 ]; then
printf "Error when executing command: $command "
exit $ERROR_CODE
fi
}
但是,这部法律并没有实现我的愿望。 我在哪里犯错误?
我想写这样的法典:
command="some command"
safeRunCommand $command
safeRunCommand() {
cmnd=$1
$($cmnd)
if [ $? != 0 ]; then
printf "Error when executing command: $command "
exit $ERROR_CODE
fi
}
但是,这部法律并没有实现我的愿望。 我在哪里犯错误?
固定代码如下:
#!/bin/ksh
safeRunCommand() {
typeset cmnd="$*"
typeset ret_code
echo cmnd=$cmnd
eval $cmnd
ret_code=$?
if [ $ret_code != 0 ]; then
printf "Error: [%d] when executing command: $cmnd " $ret_code
exit $ret_code
fi
}
command="ls -l | grep p"
safeRunCommand "$command"
现在,如果你看一下这一法典,我改变的几件事情是:
typeset
is not necessary, but it is a good practice. It makes cmnd
and ret_code
local to safeRunCommand
ret_code
is not necessary, but it is a good practice to store the return code in some variable (and store it ASAP), so that you can use it later like I did in printf "Error: [%d] when executing command: $command " $ret_code
safeRunCommand "$command"
. If you don’t then cmnd
will get only the value ls
and not ls -l
. And it is even more important if your command contains pipes.typeset cmnd="$*"
instead of typeset cmnd="$1"
if you want to keep the spaces. You can try with both depending upon how complex is your command argument.Note: Do remember some commands give 1 as the return code even though there isn t any error like grep
. If grep
found something it will return 0, else 1.
提 出
safeRunCommand() {
"$@"
if [ $? != 0 ]; then
printf "Error when executing command: $1 "
exit $ERROR_CODE
fi
}
应为<代码>$cmd,而不是$($cmd)
。 在我的盒子上,它做了一些细微的工作。
Your script works only for one-word commands, like ls. It will not work for "ls cpp". For this to work, replace cmd="$1"; $cmd
with "$@"
. And, do not run your script as command="some cmd"; safeRun command
. Run it as safeRun some cmd
.
此外,当你必须打折你的印章时,用——x国旗执行。 [bash -x s.sh]。
你的文字有几个错误。
职能(附属机构)应在试图指定之前宣布。 你可能希望(......)返回,但不要离开(......)你,让呼吁的团体检验某一指挥的成功或失败。 除此以外,你没有抓住ERROR_ 因此,CODE一直为零(未界定)。
打破你可变的参考资料,同时用粗略的镜子,也是好的做法。 您的法典不妨考虑:
#!/bin/sh
command="/bin/date -u" #...Example Only
safeRunCommand() {
cmnd="$@" #...insure whitespace passed and preserved
$cmnd
ERROR_CODE=$? #...so we have it for the command we want
if [ ${ERROR_CODE} != 0 ]; then
printf "Error when executing command: ${command}
"
exit ${ERROR_CODE} #...consider return() here
fi
}
safeRunCommand $command
command="cp"
safeRunCommand $command
通常的想法是管理指挥,然后使用<代码>$?,以获得撤离代码。 然而,有时你有多种情况需要获得出境法。 例如,你可能需要隐瞒其产出,但还是要退回,或印刷出勤守则和产出。
ec() { [[ "$1" == "-h" ]] && { shift && eval $* > /dev/null 2>&1; ec=$?; echo $ec; } || eval $*; ec=$?; }
这将使你可以选择抑制你想要的撤离守则的指挥产出。 当产出被抑制用于指挥时,撤离法将直接按职能归还。
我个人希望把这一职能放在我的.bashrc。
下面我展示了你能够利用的几种方法:
# In this example, the output for the command will be
# normally displayed, and the exit code will be stored
# in the variable $ec.
$ ec echo test
test
$ echo $ec
0
# In this example, the exit code is output
# and the output of the command passed
# to the `ec` function is suppressed.
$ echo "Exit Code: $(ec -h echo test)"
Exit Code: 0
# In this example, the output of the command
# passed to the `ec` function is suppressed
# and the exit code is stored in `$ec`
$ ec -h echo test
$ echo $ec
0
Solution to your code using this function
#!/bin/bash
if [[ "$(ec -h ls -l | grep p )" != "0" ]]; then
echo "Error when executing command: grep p [$ec]"
exit $ec;
fi
您还应指出,您将看到的出勤代码为<编码>grep。 指挥工作正在进行之中,这是最后一名指挥官被处决。 http://code>ls。
I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...
How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks
I like to write bash shell scripts on my iPhone, put them in an app folder with an icon, plist, etc., so they execute like apps by tapping their icon on SpringBoard. This is not interactive like ...
From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...
I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...
I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...
I m 试图撰写一个双面文字,处理一个文件清单,其名字在投入档案中每栏一栏储存,就像一个一样。
Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...