Reading input with spaces in csh Shell Script

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

#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

If I enter the string "rr r" in "input", $ TEST will only accept "r". I want to be able to set $ TEST to "rr r". Is it possible? If I enter a string like "1 1 1", I get an error message:

: The variable name must begin with a letter.

What is the reason for this?

+3
source share
1 answer

This is because you do not use quotation marks in your expression SET. When you enter "r r r"as your input, two different options (without quotes and quotes) are equivalent:

set TEST=$<    :is equivalent to:  set TEST=r r r
set TEST="$<"  :is equivalent to:  set TEST="r r r"

TEST "r" r "" (!). TEST - "r r r". , csh , :

set a=1 b=2 c d=4

, SET. , , :

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

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

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

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

, , "1 1 1", , :

set TEST=1 1 1

csh , TEST, "1", 1, , . :

set TEST="1 1 1"

, .

+4

Source: https://habr.com/ru/post/1706868/


All Articles