2013-03-15 220 views
0

您好,我是新手腳本,我在這裏與問題,我無法將命令行變量傳遞給我的腳本。shell腳本中的命令行參數

biz$: ./myproject.sh -x file2 

我(給)myproject有以下內容:

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.   

請幫我解決這個問題,我不能夠移動furthur不解決這個問題,我是新來的傢伙腳本。我不能僞裝$ 2 $ 3 $ 7..Experts pls我需要你的建議。

+0

你的例子不清楚。你想在awk代碼或bash代碼中使用'file2'嗎? – user000001 2013-03-15 06:50:05

+0

bash code..myproject.sh調用file2(我已更改其名稱對不起) – biz 2013-03-15 06:52:46

+1

對不起,但您的代碼仍然不清楚。在bash部分有錯誤(在這段時間之後沒有'do',''''中的空格和'case'沒有被終止。下半部分似乎是'awk'腳本的一部分,同樣錯誤(缺少幾個'}')。你想從'awk'讀取一個'bash'變量嗎? – cdarke 2013-03-15 08:56:58

回答

4

我相信你正在使用BASH,並且你想要在你的腳本中獲得命令行參數到兩個變量中。在這種情況下,專業方法是使用'getopts'

請參閱此鏈接:bash command line arguments瞭解更多詳情。

0
#!/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: "[email protected]" 
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:/