sh script from another shell

I wanted to try zsh shell, however certain scripts are now not running. Isn't the script interpreted by whatever you have defined in your script eg. #!/bin/sh , rather than whatever shell your in? What am I missing?

Thanks
 
bbzz said:
I wanted to try zsh shell, however certain scripts are now not running. Isn't the script interpreted by whatever you have defined in your script eg. #/bin/sh , rather than whatever shell your in?

It should be, but note that it has to be #!/bin/sh in the first line of the script.
 
So why do the scripts fail? They were fine running in tcsh, as soon as I switched to zsh it reports errors. Then back to tcsh and it's fine. And by errors I mean certain things that would most likely be programmed differently in [tc]sh. So I know that zsh is interpreting them.

Example:

this is one line from script:

Code:
MEM_PAGE=$(sysctl -n hw.pagesize)
MEM_SIZE=$(( $(sysctl -n vm.stats.vm.v_page_count) * $(MEM_PAGE) / 1024 /1024 ))

When run in tcsh its ok; when run from zsh shell, it reports arithmetic expression expected...etc..etc.
 
How are you running the scripts? They should be flagged as executables (chmod +x file) and run like any other executable file. Sourcing the with source or . will not work. Did you fix those shebang lines like wblock@ suggested?
 
shebang matters
Code:
### catch when shebang is actually used with nonexistent executable
$ cat >foo.sh <<EOF && chmod +x foo.sh
#!/nonexistent
ps p $$
EOF

$ tcsh -c ./foo.sh
./foo.sh: Command not found.
Exit 1

$ zsh -c ./foo.sh
zsh:1: ./foo.sh: bad interpreter: /nonexistent: no such file or directory
Exit 127

### now, let's break it by removing `!'
$ sed -i '' s/\!// foo.sh
$ cat foo.sh
#/nonexistent
ps p $$

$ tcsh -c ./foo.sh
  PID STAT TT     TIME COMMAND
33882 S+    2  0:00.00 /bin/csh ./foo.sh

$ zsh -c ./foo.sh
  PID STAT TT     TIME COMMAND
33885 S+    2  0:00.00 sh ./foo.sh

### or just nuke it
$ sed -i '' 1d foo.sh
$ cat foo.sh
ps p $$

$ tcsh -c ./foo.sh
  PID STAT TT     TIME COMMAND
33895 S+    2  0:00.00 /bin/sh ./foo.sh

$ zsh -c ./foo.sh
  PID STAT TT     TIME COMMAND
33897 S+    2  0:00.00 sh ./foo.sh
And it's actually a documented behavior.
 
Yes, ofc, shebang is there (there, edited first post). Flagged with +x.

The actual error it reports, for that line is:
" arith: syntax error: " 3038353 * 4096 / 1024 /1024 "
etc, etc.
 
So? Unless you show the affected line and how you run the script we're not getting anywhere. That error message is not enough
Code:
$ zsh -c 'echo $(( 3038353 * 4096 / 1024 /1024 ))'
11868
$ sh -c 'echo $(( 3038353 * 4096 / 1024 /1024 ))'
11868
For one, zsh's arith is compatible with sh's arith but has a few extensions, e.g.
Code:
$ echo $((++i))
1
$ echo $((++i))
2
$ echo $((++i))
3

$ (( 1 < 2 )); echo $?
0
$ (( 1 > 2 )); echo $?
1
 
bbzz said:
MEM_PAGE=$(sysctl -n hw.pagesize)
MEM_SIZE=$(( $(sysctl -n vm.stats.vm.v_page_count) * $(MEM_PAGE) / 1024 /1024 ))
Try to drop braces around MEM_PAGE as it's not a command but a variable, e.g.
[cmd=]MEM_SIZE=$(( $(sysctl -n vm.stats.vm.v_page_count) * MEM_PAGE / 1024 /1024 ))[/cmd]

I get below error under sh(1)
Code:
$ MEM_PAGE=$(sysctl -n hw.pagesize)
$ MEM_SIZE=$(( $(sysctl -n vm.stats.vm.v_page_count) * $(MEM_PAGE) / 1024 /1024 ))
MEM_PAGE: not found
arithmetic expression: expecting primary: " 1010646 *  / 1024 /1024 "
 
Found the error! Started zsh session without ~/.zshrc and it worked fine. So the issue was in my zsh config file. Unfortunately I'm posting from my laptop so I'm not going to copy the whole file (which had quite a bit of settings in there).
Long story short, there is a line for sysutils/cw coloring program I use
Code:
export PATH="/usr/local/lib/cw:"....
After removing this it works. For some reason it screws up everything (not sure why).
I guess it's because of vmstat in there.

Thanks for all the help, appreciate it.

@swallowtail_butterfly
That was typo; it's ${MEM_PAGE} not $(MEM_PAGE) :)
 
bbzz said:
Found the error! Started zsh session without ~/.zshrc and it worked fine. So the issue was in my zsh config file. Unfortunately I'm posting from my laptop so I'm not going to copy the whole file (which had quite a bit of settings in there).
Long story short, there is a line for /sysutils/cw coloring program I use
Code:
export PATH="/usr/local/lib/cw:"....
After removing this it works. For some reason it screws up everything (not sure why).
I guess it's because of vmstat in there.

Thanks for all the help, appreciate it.

@swallowtail_butterfly
That was typo; it's ${MEM_PAGE} not $(MEM_PAGE) :)

you don't need quotes with PATH
 
Question about a book....

I was going to purchase this book today..

http://shop.oreilly.com/product/9780596005955.do?sortby=publicationDate

but I see it is Classic Shell scripting...so that means it is Bash scripting correct?

I mainly use csh as my shell system on all my servers so I was thinking this might not be a good book to get? I could be wrong and if I am please let me know.

Is there a book that someone can recommend to learn basic scripting/programing in the csh shell. Thanks!
 
Many of us use csh(1) for interactive use, but write shell scripts in sh(1). As shown above, scripts with the shebang line figure out what shell to run in by themselves.
 
Back
Top