将hash-sign-starting-string参数传递给Shell脚本 [英] Pass hash-sign-starting-string parameter to a shell script
问题描述
我尝试将两个#
符号之间包含的字符串传递给ksh脚本:
I try to pass a string containted between two #
sign to a ksh script :
Will #> ./a.ksh #This is a string#
Will #>
不提示任何内容,执行的输出将是:
Nothing is prompted, the execpeted output would be :
Will #> ./a.ksh #This is a string#
#This is a string#
在a.ksh
脚本下面.
a.ksh
echo $1
echo "$1"
echo ${1}
echo "${1}"
我尝试用我知道的任何方法来保护我的
$1
变量,但是我 无法显示以#
开头的任何内容.
I have tried to protect my
$1
variable by any means I knew but I can't get anything starting with#
to be displayed.
以下追踪者无效:
#> ./a.ksh #Hello
#> ./a.ksh #
但是这样做:
#> ./a.ksh Hello#
任何能解释我为什么以及如何使它像被激怒一样工作的贝壳大师吗?
Any shell guru that could explain me why and how to get this to work as excpeted ?
我可以使用\#This String#
或"#This String#"
转义那些字符串,但是我想知道为什么#
不能单独打印.
I can escape those string using \#This String#
or "#This String#"
but I wonder why does #
doesn't print by itself.
推荐答案
#
被解释为注释的开始.解释器将忽略所有注释.
#
is interpreted as the start of a comment. All comments are ignored by the interpreter.
您有两个选择:
-
引用该字符串以使其成为文字:
Quote the string to make it a literal:
./a.ksh "#This is a string#"
让脚本读取用户输入而不是参数输入:
Have your script read input from the user instead of a parameter:
这是一个例子:
#!/bin/ksh
echo "Enter your text:"
read -r input
echo "You entered: $input"
然后先运行程序,然后输入以下数据:
then run the program first and enter your data after:
$ ./a.ksh
Enter your text:
#This is a string#
You entered: #This is a string#
这篇关于将hash-sign-starting-string参数传递给Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!