关于bash:shell脚本中的命令行参数

command line argument in shell scripting

嗨,我是脚本新手,这里有个问题,我不能将命令行变量传递给我的脚本。

1
biz$: ./myproject.sh -x file2

我的(给定的)我的项目包含以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 Type ="" //here i pass first argument
 while [ $# -gt 0]
 case"$1" in
       -x)        shift; type ="x">&2;shift ;;
       -y)        shift; type ="y">&2;shift ;;
 ###################################################
 BEGIN{                            
       if ($7 == '/'){
           if ($2 !="zzzz"){
               printf ("error",$0);

           if ($3 < 111){
               printf ("error", $0);
         }

 file ="" //here i want to pass my argument file2.

请帮我解决这个问题,如果不解决这个问题,我就无法继续前进,我是新来的编写脚本的人。我不能存2美元3美元7。专家们,我需要你的建议。


我相信您使用的是bash,并且您希望在脚本中将命令行参数获取为两个变量。在这种情况下,专业的方法是使用"getopts"

有关详细信息,请参阅以下链接:bash命令行参数。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/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"$1" 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=$1; shift                            ;;
   -y)  shift; Type=y              # Get rid of -y only
                                                         ;;
  one)  if ["$7" = '/' ]    # Space around = for tests
        then
          echo error $0 >&2
        fi
        if ["$2" != zzzz ]
        then
          echo $2 is not equal to zzzz
        fi
        if ["$3" -lt 111 ]          # -lt is less than
        then
          echo"$3 is less than 111"
        fi
        break                 # break out of while loop
                                                         ;;
   esac
   echo Cmd Ln Args left:"$@"
 done
 echo file: $file, Type: $Type, \$3: $3, \$7: $7
####################################################
# The code below is awk code. Its functionality was
#   placed under case one above
# BEGIN{                            
#       if ($7 == '/'){
#           if ($2 !="zzzz"){
#               printf ("error",$0);
#
#           if ($3 < 111){
#               printf ("error", $0);
#         }
#
# 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, $3: 110, $7: /