在csh Shell脚本中读取带有空格的用户输入

发布于 2024-07-17 20:21:53 字数 471 浏览 4 评论 0原文

我有一个脚本,用户应该能够输入带空格的字符串。 到目前为止,我已经:

#bin/csh

echo "TEST 1"
echo -n "Input : "
set TEST = $<

echo "Var | " $TEST

set TEST=`echo $TEST`

echo "Var after echo | " $TEST

set TEST=`echo $TEST | sed 's/ /_/g'`

echo "Var after change | " $TEST

如果我在“input”处输入字符串“rr r”,$TEST 将只接受“r”。 我希望能够将 $TEST 设置为“rr r”。 这可能吗? 如果我输入像“1 1 1”这样的字符串,我会收到错误:

set:变量名必须以a开头 信。

这是什么原因呢?

I have a script where the user should be able to enter a string with spaces. So far I have:

#bin/csh

echo "TEST 1"
echo -n "Input : "
set TEST = 
lt;

echo "Var | " $TEST

set TEST=`echo $TEST`

echo "Var after echo | " $TEST

set TEST=`echo $TEST | sed 's/ /_/g'`

echo "Var after change | " $TEST

If I enter the string "r r r" at "input", $TEST would only take "r". I want to be able to set $TEST to "r r r". Is this possible?
If I enter a string like "1 1 1" I get an error:

set: Variable name must begin with a
letter.

What's the reason for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩心不撩汉 2024-07-24 20:21:53

这是因为您没有在 SET 语句中使用引号。 当您输入 "rr r" 作为输入时,两种不同的变体(不带引号和带引号)等效于:

set TEST=
lt;    :is equivalent to:  set TEST=r r r
set TEST="
lt;"  :is equivalent to:  set TEST="r r r"

第一个只是将 TEST 设置为 " r"r""(两次!)。 第二个将 TEST 设置为 "rr r"。 这是因为 csh 允许您执行多项赋值,例如:

set a=1 b=2 c d=4

因此您需要使用 SET 的带引号的变体。 检查以下记录以了解其工作原理:

[pax ~]$ set x=
lt; ; echo .$x.
hello
.hello.

[pax ~]$ set x="
lt;" ; echo $x ; echo .$b.$c.
a b c
.a b c.
b: Undefined variable.

[pax ~]$ set x=
lt; ; echo $x ; echo .$b.$c.
a b c
.a.
...

[pax ~]$ set x=
lt; ; echo $x ; echo .$b.$c.
a b=7 c=urk!
.a.
.7.urk!.

您收到输入 "1 1 1" 所描述的错误的原因是因为您正在有效执行:

set TEST=1 1 1

csh 认为这意味着您要创建设置为 "1" 的变量 TEST,后跟变量 1,这不会t 以字母开头,因此不允许。 使用引用的变体,这将变为:

set TEST="1 1 1"

它将执行您所期望的操作。

It's because you're not using quotes in your SET statement. When you enter "r r r" as your input, the two different variants (unquoted and quoted) are equivalent to:

set TEST=
lt;    :is equivalent to:  set TEST=r r r
set TEST="
lt;"  :is equivalent to:  set TEST="r r r"

The first of those simply sets TEST to "r" and r to "" (twice!). The second sets TEST to "r r r". That's because csh lets you do multiple assignments like:

set a=1 b=2 c d=4

So you need to use the quoted variant of SET. Examine the following transcript to see how it works:

[pax ~]$ set x=
lt; ; echo .$x.
hello
.hello.

[pax ~]$ set x="
lt;" ; echo $x ; echo .$b.$c.
a b c
.a b c.
b: Undefined variable.

[pax ~]$ set x=
lt; ; echo $x ; echo .$b.$c.
a b c
.a.
...

[pax ~]$ set x=
lt; ; echo $x ; echo .$b.$c.
a b=7 c=urk!
.a.
.7.urk!.

The reason you're getting the error you describe with input of "1 1 1" is because you're effectively executing:

set TEST=1 1 1

and csh is taking this to mean that you want to create the variable TEST set to "1" followed by the variable 1, which doesn't start with a letter, hence not allowed. With the quoted variant, this becomes:

set TEST="1 1 1"

which will do what you expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文