Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Can anyone please tell me what exactly this script does?
#! /bin/sh
test –f /usr/bin/sshd || exit 0
case “$1” in
start)
echo –n “Starting sshd: sshd”
/usr/sbin/sshd
echo “.”
;;
stop)
echo –n “Stopping sshd: sshd”
kill `cat /var/run/sshd.pid`
echo “.”
;;
restart)
echo –n “Stopping sshd: sshd”
kill `cat /var/run/sshd.pid`
echo “.”
echo –n “Starting sshd: sshd”
/usr/sbin/sshd
echo “.”
;;
*)
echo “Usage: /etc/init.d/sshd start|stop|restart”
exit 1
;;
esac
I want to know what exactly this part:
#! /bin/sh
test –f /usr/bin/sshd || exit 0
case “$1” in
start)
echo –n “Starting sshd: sshd”
/usr/sbin/sshd
echo “.”
;;
does because the other part is the same!
Please ;)
Which other part is the same? The way that script works is it checks the value of $1
, which is the first parameter to the script supplied on the command-line. If it's 'start', then the part after start)
is executed. If it's 'stop', then the part after stop)
is executed. If it's 'restart', then the part after restart)
is executed.
Line by line for that first part:
#! /bin/sh
Hey, it's a shell script! Specifically, execute this script using the sh
shell.
test –f /usr/bin/sshd || exit 0
Is there a file called /usr/bin/sshd
? If not, exit with a 0 return status.
case “$1” in
Check the value of $1
, the first command-line option.
start)
If $1
is 'start'...
echo –n “Starting sshd: sshd”
Print "Starting sshd: sshd
".
/usr/sbin/sshd
Execute /usr/sbin/sshd
.
echo “.”
Print ".
".
;;
Exit the case
statement.
sshd writes its process ID to the file in /var/run. The backticks
cause the script inside them to be executed by the shell, and the result is used in its place.
The net result is kill [sshd pid]
The part you mention starts the "sshd" program. It's the Secure Shell (SSH) daemon (server process).
Depending on the command-line argument, your script starts, stops or restarts the SSH server. $1
represents the command-line argument, and this value gets compared to the different possible values between case
and esac
.
The test –f /usr/bin/sshd
part returns true
if the file exists. The ||
is an or, so the || exit 0
gets executed (ending the script) only if the first part returned false.
In the case "$1"
part, the $1
is the first argument passed to the script.
"I want to know what exactly this part... does because the other part is the same!"
start) assumes that sshd is not already started, and starts it.
This is different from restart), which first stops the sshd process (as Joe describes), and then starts it again.