简体   繁体   English

关于通过shell脚本问题传递参数

[英]regarding argument passing through shell script issue

Am new to shell script. 是Shell脚本的新手。

my requirement is i have a test.sh shell script file i dont know how to pass args through shell script also. 我的要求是我有一个test.sh shell脚本文件,我也不知道如何通过shell脚本传递args。

i my shell script i want o get some of the data via args like 我我的shell脚本我想通过像这样的参数获取一些数据

perzonalize browser='FF'

i tried $ test.sh perzonalize browser='FF' and inside script i did 我试过$ test.sh perzonalize browser='FF'并且在脚本里面

      echo $1 $2 

but it prints like perzonalize browser=FF .i need that quote browser='FF' 但它的打印效果像perzonalize browser=FF 。我需要使用quote browser='FF'

How it is possible 怎么可能

Quoting is very critical in shell scripts. 在shell脚本中,报价非常关键。

You need to call your script as: 您需要将脚本调用为:

perzonalize "browser='FF'"

And echo 1st argument as: 并将第一个参数作为回显:

echo "$1"

which will print string with single quotes: 这将打印带有单引号的字符串:

browser='FF'
echo "$@"

Is probably what you want. 可能就是您想要的。 It passes each argument along separately. 它分别传递每个参数。 According to the man page: "$@" is equivalent to "$1" "$2" ... * 根据手册页: “ $ @”相当于“ $ 1”“ $ 2” ... *

The important thing is the double quotes. 重要的是双引号。 You should almost always expand your shell veriables "$like_this". 您几乎应该总是扩展您的shell验证字“ $ like_this”。 For some rare cases you want 在某些罕见的情况下,您想要

echo "$*"

This will produce just one string consisting of all your original arguments together, separated by whitespace. 这将只产生一个字符串,其中包含所有原始参数,并用空格分隔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM