0

I'm trying to create a script to to do simple things. I need to prompt the user to reply to a question, typing yes or no. The script is written for csh, but does not work when default user shell is bash. My environment is Red Hat Enterprise Linux 5

#!/bin/csh -f
echo -n Type yes to continue
set answer = $<
#...

This code works fine with csh but not with bash where it prints the following error:

syntax error near unexpected token 'newline'
bash 'set answer =$<'

I really need to have the same script working for both shell (I thought it was the purpose of putting #!/bin/csh at the beginning of the file!)

I don't really know how to modify my script to make it working in bash. Could you help me please? Thanks a lot for your help.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
user1657743
  • 141
  • 1
  • 2
  • 5

1 Answers1

3

It would be hard to think of two more different shells than csh and bash. They are different languages, you cannot expect csh code to work in bash, or the other way around.

In bash you read from STDIN using the shell builtin read, in csh you use the construct $<. Different languages.

I really need to have the same script working for both shell Why? When you place #!/bin/csh at the start of the script then it will run the c-shell, use #!/bin/bash then it will run bash. Common mistake is to have white-space or some other character before the #!, they must be absolutely the very first two bytes in the file.

cdarke
  • 42,728
  • 8
  • 80
  • 84
  • 1
    ... But it would be much better to port it to Bash and abandon Csh. See also http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ – tripleee Jan 06 '13 at 21:53
  • Agreed, but there are a lot of legacy scripts out in the wild writtten in csh and it costs money to convert them. – cdarke Jan 07 '13 at 09:36