#!/bin/bash cat << EOF What information would you like to see: (cpu) show cpu information; (disk) show disk information; (mem) show memory information; (quit) quit =================================== EOF
read -p "Enter a option: " option
while [ $option != 'cpu' -a $option != 'disk' -a $option != 'mem' -a $option != 'quit' ]; do read -p "Wrong option. Enter again: " option done
while [ -n $option ]; do if [ $option == "cpu" ]; then lscpu elif [ $option == "mem" ]; then cat /proc/meminfo elif [ $option == 'disk' ]; then fdisk -l elif [ $option == 'quit' ]; then echo"Quit" exit 0 fi read -p "See anything? [cpu,disk,mem,quit] " option done
usage() { echo"USAGE: service {start|stop|restart|status}" }
start() { if [ -e $LOCK ]; then echo"service has already run...exit" exit 1 fi echo"Attempting to start the service..." touch$LOCK if [ $? -eq 0 ]; then echo"Service started." exit 0 else echo"Something goes wrong.." rm -f $LOCK exit 2 fi }
stop() { if [ -e $LOCK ]; then echo"Stopping service..." rm -f $LOCK if [ $? -eq 0 ]; then echo"Service stopped." exit 0; fi else echo"Service is not running..." fi }
if [ -n $1 ]; then if [ $1 == 'start' -o $1 == 'stop' -o $1 == 'restart' -o $1 == 'status' ]; then case"$1"in start) start ;; stop) stop ;; status) if [ -e $LOCK ]; then echo"Service is running." else echo"Service is not running" fi exit 0 ;; restart) stop start ;; *) usage exit 0 ;; esac else echo -n -e "Invalid option: $1\n" usage fi fi
[root@WWW ~]# for n in {1..9}; do echo $n; done 1 2 3 4 5 6 7 8 9 [root@WWW ~]# seq 1 2 10 1 3 5 7 9
接下是一个ls返回列表的示例:
1 2 3 4 5 6 7 8 9 10
#!/bin/bash for file in $(ls /var/); do if [ -f /var/$file ]; then echo"Regular File" elif [ -L /var/$file ]; then echo"Symbolic File" elif [ -r /var/$file ]; then echo"Directory" fi done