0

Hey everyone i have this issue where i have a sample program in csh (i know its not the best language to use for scripting but i dont have an option here) its as foll:

#!/bin/csh    
echo 'please enter values' 
read a 
read -e b
echo "My values are $a and $b" 

As you might see my second read is going to take a file input. This is needed as my actual program will have that; now what i want to do is to convert this to alias however when i execute the above script i get the foll. output

please enter values
read: Command not found
read: Command not found
a: Undefined variable

How can i fix this and also i want to convert this all 4 lines into a multiline alias. I have written single line alias in the past with no issues but not sure about multiline. if someone can help out i'll be really grateful

3
  • What implementation of csh are you using? neither BSD csh nor the Tenex tcsh has a read function AFAIK. I notice you don't show a "shebang" at the top of the code - which makes me suspect you are executing it from bash, and it is actually being interpreted by bash. Aug 28, 2018 at 19:34
  • i had read it somewhere that if u execute the file ie ./file.csh the read command would work unlike when you just source the file and thats how i was able to use it till now. and as far as the "shebang" i didnt mention it but when i do echo $SHELL i get /bin/tcsh so i am assuming its been interpreted as tcsh and not bash Aug 28, 2018 at 21:32
  • Please add an appropriate shebang such as #!/bin/csh or #!/usr/bin/env csh to your script and update your question with the output of ./file.csh after that. FWIW echo $SHELL will usually tell you what your login shell is set to - but that's not necessarily the interactive shell that you are running. Aug 28, 2018 at 22:37

1 Answer 1

1

As you have discovered, the C shell does not have a built-in read function similar to that of Bourne-type shells.

I am not proficient in csh synatax but as far as I know, the nearest equivalent is to use the special variable $< . From man csh:

       $<      Substitutes a line from the standard input, with no further
               interpretation.  It can be used to read from the keyboard
               in a shell script.

e.g.

$ cat myscript.csh
#!/bin/csh

echo 'please enter values'
set a = $<
set b = $<
echo "My values are $a and $b"

which gives

$ ./myscript.csh
please enter values
123
abc
My values are 123 and abc

Note that there are implementation-dependent differences in how assignments from $< handles multi-word input - in particular, the Tenex tcsh does not quote input by default, instead requiring $<:q to get the same behavior as BSD csh. Also there is (as far as I know) no built-in readline editing support equivalent to the bash shell's read -e.

As far as multi-line aliases are concerned, they do seem to work:

% alias jo '\
echo "please enter values "\
set a = $< \
set b = $< \
echo "My values are $a and $b"'
%
% alias
jo
echo "please enter values "
set a = $<
set b = $<
echo "My values are $a and $b"

Testing it

% jo
% please enter values
123
foo bar
My values are 123 and foo bar
%

however it's not a feature that I would recommend relying on.

1
  • wow thats a lot to take....but thanks a lot for that.... Aug 29, 2018 at 23:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .