Shell 脚本参数 [英] Shell script arguments
本文介绍了Shell 脚本参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚开始在 Unix 中编写 shell 脚本,所以我完全是个新手
I just started writing shell scripts in Unix so, I am a total newbie
我想读取用户运行脚本时给出的参数例如:
I want to read the arguments given when the user run the script ex:
sh script -a abc
我想读取用户给 abc
的参数 -a
.
I want to read for argument -a
user gave abc
.
到目前为止我的代码:
if ( $1 = "-a" )
then var=$2
fi
echo $var
出现错误.
推荐答案
Bash 使用名为 test 执行布尔测试,但该程序主要通过其别名 [
使用.
Bash uses an external program called test to perform boolean tests, but that program is used mostly via its alias [
.
if ( $1 = "-a" )
应该变成
if [ $1 = "-a" ]
如果你使用 [
或
if test $1 = "-a"
这篇关于Shell 脚本参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文