2022-02-11 · 3

Shell Script

linuxshell

This post is over 2 years old. The content may be outdated.

-   chmod 700(755) [a.sh](http://a.sh) → ./a.sh
  • Variable assignment
    • TXT=”HelloWorld”
    • = there must be no spaces on either side
  • Variable reference
    • you can use a variable like ${TXT}
    • “${TXT}”
  • Reading input
    • read

Conditionals

  • if [ condition1 ]; then
    • the command to run when condition 1 is true
  • elif [ condition2 ]; then ←- can be added when a condition is needed
    • the command to run when condition 1 is false and condition 2 is true
  • else
    • the command to run when both condition 1 and condition 2 are false
  • fi

Comparison table

Numeric comparison / description / string comparison / description / file comparison / description

<table style="border-collapse: collapse; width: 100%;" border="1" data-ke-align="alignLeft"><tbody><tr><td>A -eq B</td><td>Is A equal to B</td><td>A = B</td><td>Are strings A, B equal</td><td>-d file</td><td>Does the file exist and is it a directory</td></tr><tr><td>A -ge B</td><td>Is A greater than or equal to B</td><td>A != B</td><td>Are strings A, B different</td><td>-e file</td><td>Does the file exist</td></tr><tr><td>A -gt B</td><td>Is A greater than B</td><td>A < B</td><td>Is string A less than B (by byte)</td><td>-f file</td><td>Does the file exist and is it a file</td></tr><tr><td>A -le B</td><td>Is A less than or equal to B</td><td>A > B</td><td>Is string A greater than B (by byte)</td><td>-r file</td><td>Does the file exist and is it readable</td></tr><tr><td>A -lt B</td><td>Is A less than B</td><td>-n A</td><td>Is A's string length greater than 0</td><td>-s file</td><td>Does the file exist and is it non-empty</td></tr><tr><td>A -ne B</td><td>Are A and B not equal</td><td>-z A</td><td>Is A's string length 0</td><td>-w file</td><td>Does the file exist and is it writable</td></tr></tbody></table>

case

case variable in
	pattern1)
		command to run if pattern1 matches;;
	pattern2)
		command to run if pattern2 matches;;
	*)
		command to run if none of the patterns above match;;
esac

Loops

for

for variable in data-to-iterate
do
	command to run repeatedly until the data ends
done

while

while condition
do
	command to run repeatedly while the condition is true
done
  • Host-name output loop
#!/bin/bash

for SVR in 22.1.24.10 22.1.24.20 22.1.24.30
do
	echo "This is ${SVR} Server"
	ssh ${SVR} "hostname"
done
  • Loop using seq
for NUM in $(seq 1 3)
do
	echo "Printing [${NUM}]"
	echo ""
done
  • Making an nginx start/restart/stop/status shell
#!/bin/bash

 ./nginx.sh start
 ./nginx.sh restart
 ./nginx.sh stop

CMD=$1

case "$CMD" in
start)
	systemctl start nginx;;
restart)
	systemctl restart nginx;;
stop)
	systemctl stop nginx;;
status)
	systemctl status nginx;;
*)
	echo "wrong code";;
esac
  • Example 1: output a message if the load is 5 or more

  • Example 2: output nginx's process number, then shut down the system

    • ps - ef | grep nginx
    • pgrep -l nginx / pgrep -a nginx / ps axfwwww | grep nginx
    • systemctl status nginx | grep nginx→
  • Example 3: after finding out the hostname, determine whether it's a kerberos server

  • Example 4: change network adapter settings

Try making the opposite case too

  • Example 5: a one-line script that checks whether /var/www/html exists and, if so, whether it's a file
  • Example 6: a script using crontab to run a shell file that reports the system time every minute, outputting "happy day" on the hour
  • Example 7: every 5 minutes, create the /var/log/apache2/access.log file as ~ .bak(YYYYMMDDmm), save it to /var/log/apache2/backuplog, and then clear the contents of access.log
  • A setting that applies iptables NAT at boot and blocks the forwarding policy for TCP ports 1~80

Original (Korean): tistory — published 2022-02-11, migrated to this blog. This translation was generated with the help of AI.

Comments

Delete this comment?

Related posts

Shell Script · 나봄하랑