bash shell 脚本中的命令行参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15426344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
command line argument in shell scripting
提问by biz
HI all i am newbie to scripting, i am here with problem ,that i am not able to pass command line variable to my script .
大家好,我是脚本的新手,我在这里遇到问题,我无法将命令行变量传递给我的脚本。
biz$: ./myproject.sh -x file2
My(given) myproject has these contents:
我的(给定的)myproject 有这些内容:
Type ="" //here i pass first argument
while [ $# -gt 0]
case "" in
-x) shift; type = "x" >&2;shift ;;
-y) shift; type = "y" >&2;shift ;;
###################################################
BEGIN{
if ( == '/'){
if ( != "zzzz"){
printf ("error",#!/bin/sh
# First line above, if this is a bourne shell script
# If this is a bash script use #!/bin/bash
# Assume this script is called from the command line with the following:
# ./myproject.sh -x file2 -y one two 110 four five six /
#Type ="" \ here i pass first argument
# Comments are preceeded with # followed by a space
# No spaces around = for assignment of values
# Empty string "" not necessary
Type= # Here i pass first argument
#while [ $# -gt 0] # Spaces required just inside []
while [ $# -gt 0 ]
do
case "" in
# -x) shift; type = "x" >&2;shift ;;
# >&2 Redirects standard out to standard error (stdout, stderr)
# and usually is not needed unless explicitly generating error
# messages
# Type is not the same as type; however, you are trying to
# load the file variable
-x) shift; file=; shift ;;
-y) shift; Type=y # Get rid of -y only
;;
one) if [ "" = '/' ] # Space around = for tests
then
echo error ##代码## >&2
fi
if [ "" != zzzz ]
then
echo is not equal to zzzz
fi
if [ "" -lt 111 ] # -lt is less than
then
echo " is less than 111"
fi
break # break out of while loop
;;
esac
echo Cmd Ln Args left: "$@"
done
echo file: $file, Type: $Type, $3: , $7:
####################################################
# The code below is awk code. Its functionality was
# placed under case one above
# BEGIN{
# if ( == '/'){
# if ( != "zzzz"){
# printf ("error",##代码##);
#
# if ( < 111){
# printf ("error", ##代码##);
# }
#
# file = " " //here i want to pass my argument file2.
OUTPUT:
Cmd Ln Args left: -y one two 110 four five six /
Cmd Ln Args left: one two 110 four five six /
error ./myproject.sh
two is not equal to zzzz
110 is less than 111
file: file2, Type: y, : 110, : /
);
if ( < 111){
printf ("error", ##代码##);
}
file = " " //here i want to pass my argument file2.
Please help me out to solve this, i am not able to move furthur without solving this, i am new guy to scripting. I cant cange $2 $3 $7..Experts pls i need your suggestion.
请帮我解决这个问题,如果不解决这个问题,我将无法继续前进,我是脚本的新手。我不能改变 2 美元 3 美元 7 美元..专家请我需要你的建议。
回答by Rajgopal C
I believe that you are using BASH and you want to obtain the command line parameters into two variable inside your script. In which case, the professional approach is to use 'getopts'
我相信您正在使用 BASH 并且您希望将命令行参数获取到脚本中的两个变量中。在这种情况下,专业的方法是使用“getopts”
Please refer to this link : bash command line argumentsfor further details.
有关更多详细信息,请参阅此链接:bash 命令行参数。

