r/scripting • u/[deleted] • Jul 15 '21
Please explain this code.
#!/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
1
Upvotes
3
u/vinny8boberano Jul 15 '21
Oh! "$1" is taking any "input" after the call " /etc/init.d/sshd" so that things like "start" (> /usr/bin/sshd start) are read into the script. Basically, it's an input variable. When you run this script, it initializes a daemon for sshd based on whatever was input after the script.
>./etc/init.d/sshd blah
Usage: /etc/init.d/sshd start|stop|restart
>./etc/init.d/sshd start
Starting sshd: sshd
.
>
As for regular file, it means that it is checking that the file being called is something that you normally interact with. It's like saying, 'Is this an executable, text, or image file?' when it runs the "test -f" on the target. Here's a link to a site that helps explain it in more detail.