0

I'm new to write shell script, and I looked at some tutorial on the Internet and now I try to write my first script, it's simple:

#! /bin/csh
echo "Hello World! \a \n"
exit 0

It looks simple and correct, it will show Hello World on my terminal, but it won't be terminated -- I need to ctrl+c to stop the shell.

But I try

#! /bin/sh
echo "Hello World! \a \n"
exit 0

This can be terminated normally, I know that first line is to specify what shell you want to use to execute the script, and I used the set command to find out my default shell is /bin/csh

Other users on this server are all ok. Does everyone know how can I fix the problem? I can't use some shell scripts written by my friend.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Either the tutorial missed something very important about C shell scripts, or you missed it in the tutorial. – Ignacio Vazquez-Abrams Nov 28 '17 at 03:40
  • Hello , thanks for replying, but the same script ,in the same server, but others' account is OK – pilistar0222 Nov 28 '17 at 05:49
  • `csh` and `sh` are two different languages; in this trivial case the syntax is similar but you can't assume a construct in one will work in the other, or mean the same thing. – tripleee Nov 28 '17 at 05:56
  • 2
    If you are only just learning, I would recommend that you abandon `csh` and concentrate on learning a Bourne-compatible shell. This is a slightly controversial thing to say, but you should at least be aware that this is something to consider. The canonical FAQ is http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ – tripleee Nov 28 '17 at 05:57
  • (FWIW, I like to say that if `csh` is "C-like", then Marilyn Manson is "Marilyn-like".) – tripleee Nov 28 '17 at 05:58
  • thanks for recommendation , but i'm still confused, why the script won't terminate, I think if there is anything wrong in my account or some setting need to be modified, the hello world is simple, just an echo – pilistar0222 Nov 28 '17 at 06:09
  • 1
    How exactly are you running the script? I can't reproduce the non-termination (at least, not with `tcsh` on macOS). @tripleee is right, though; unless someone is forcing you to use `csh`, step back and walk away slowly. Learn POSIX shell instead (I suggest `dash`, as it is relatively free of extensions to the standard; once you are comfortable, you might want to try `bash`). – chepner Nov 28 '17 at 16:54

3 Answers3

0

modify the shebang from #! /bin/csh to #! /bin/csh -f it will works !! but i'm still confused why that other users on the same server, they can use the existed script that the shebang is /bin/csh without -f

is there anything wrong in my ~/.cshrc ?

  • I doubt that `.cshrc` could cause this, but csh is weird at times, please show the `.cshrc` code . How are you running the script? – cdarke Nov 28 '17 at 12:08
  • I try to remove .cshrc , but it still wont terminated QQ – pilistar0222 Nov 29 '17 at 03:22
  • You should not remove `.cshrc`, it could have important settings. Do you have a `.login`? If so, don't remove it but show it in your question. – cdarke Nov 29 '17 at 09:56
0

Sorry for post another answer again

I found that if I run the script by "source filename" it's terminated normally

so, maybe there's some reason that my child shell can't close normally and maybe in the loop??

  • 1
    i found that is the same issue like this https://stackoverflow.com/questions/35708942/tcsh-script-does-not-exit , and just remove the .history is everything fine – pilistar0222 Nov 29 '17 at 05:10
0

you can use screen command to run your script

$screen ./script.sh

you will see that the process you left is still running.

$screen -r