1752806 Members
6008 Online
108789 Solutions
New Discussion юеВ

shell script question

 
Shivkumar
Super Advisor

shell script question

Dear Sirs,

I saw a script running in crontab as:-

sh -c /home/bigguy/scripts/rose.sh >/dev/null 2>&1

The script rose.sh is written in korn shell.
The above script is running with "sh -c"

I don't understand the significance of "sh -c" option here.

Appreciate any suggestion.

Thanks,
Shiv
3 REPLIES 3
Borislav Perkov
Respected Contributor

Re: shell script question

Hi Shiv,

-c means that shell reads the command from the file or from the "string". See man sh-posix for -c.

Regards,
Borislav
Muthukumar_5
Honored Contributor

Re: shell script question

sh -c /home/bigguy/scripts/rose.sh means it is reading strings line by line and executing that.

You can run that script as,

# sh -c < /home/bigguy/scripts/rose.sh

Difference is as,

# sh ls
sh: ls: Execute permission denied.
# sh -c ls
...

It is reading ls as script instead of file. If you are not having -c option then it will take next argument as file name and after that as arguments.

hth.

Easy to suggest when don't know about the problem!
john korterman
Honored Contributor

Re: shell script question

Hi Shivkumar,

apart from what has already been stated there is also a difference with regard to process hierarchy and number of shells; try comparing the output of

$ sh -c "ps -f"
to the output of
$ ps -f


Sample output:

$ ps -f
UID PID PPID C STIME TTY TIME COMMAND
jxk 614 20419 3 12:39:42 pts/tc 0:00 ps -f
jxk 20419 20418 0 Nov 28 pts/tc 0:01 -sh
root 20418 1163 0 Nov 28 pts/tc 0:00 telnetd


The COMMAND ps -f has PID 614; the PPID (father of ps -f) is 20419.
PID 20419 is the shell itself, -sh, in which ps is executed
PID 20418, telnetd, is the father of -sh

Simple hierarchy for the above:
telnet
sh
ps -f



However.....
$ sh -c "ps -f"
UID PID PPID C STIME TTY TIME COMMAND
jxk 619 20419 2 12:43:38 pts/tc 0:00 sh -c ps -f
jxk 20419 20418 1 Nov 28 pts/tc 0:01 -sh
root 20418 1163 0 Nov 28 pts/tc 0:00 telnetd
jxk 620 619 4 12:43:38 pts/tc 0:00 ps -f

The output order looks confusing; start at
PID 620, the ps -f command, whose father is
PID 619, the command sh -c pd -f, whose father is
PID 20419, -sh whose father is
PID 20418, telnetd

Hierarchy with extra shell layer:
telnet
sh (the already available shell)
sh (the sh -c shell, extra)
ps -f (is executed in the sh -c shell)


In the example
sh -c /home/bigguy/scripts/rose.sh >/dev/null 2>&1
sh -c creates a shell layer, like in the above example, in which rose.sh will be executed.
However, if rose.sh sh has been properly written, i.e. the first line specifying which shell to use,
sh -c
does - in my opinion - not influence how rose.sh is executed.

regards,
John K.
it would be nice if you always got a second chance