2013-03-15 3 views
0

안녕하세요, 저는 스크립트에 초보자입니다. 문제가 생겨서 스크립트에 명령 줄 변수를 전달할 수 없습니다.쉘 스크립팅의 명령 줄 인수

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

+0

bash code..myproject.sh call file2 (이름 변경 죄송합니다.) – biz

+1

죄송하지만 코드가 아직 명확하지 않습니다. bash 부분에 오류가 있습니다 (잠시 후'do'가 없으며'[')에 공백이 없어지고'case'가 종료되지 않습니다. 아래 부분은'awk' 스크립트의 일부인 것으로 보이며, 다시 에러가 있습니다 (여러 개의'''가 누락되었습니다). 'awk'에서'bash' 변수를 읽고 싶습니까? – cdarke

답변

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:/