2013-03-15 3 views
0

HI all i Я новичок в написании сценариев, я здесь с проблемой, что я не могу передать переменную командной строки в свой скрипт.аргумент командной строки в командной оболочке

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 без решения этого, я новый парень для сценариев. Я наклоняю cange $ 2 $ 3 $ 7..Explts pls мне нужно ваше предложение.

+0

Ваш пример не ясен. Вы хотите 'file2' в awk-коде или в коде bash? – user000001

+0

bash code..myproject.sh call file2 (я изменил его название извините) – biz

+1

Извините, но ваш код по-прежнему не ясен. В bash-части есть ошибки (после этого не происходит 'do', пробел отсутствует в' '' 'и' case' не завершается. Нижняя часть кажется частью скрипта 'awk', опять же с ошибками в нем (несколько' '' missing). Вы хотите прочитать переменную 'bash' из' awk'? – 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:/
Смежные вопросы