常用 shell 脚本
来源:互联网 发布:windows nginx重启 编辑:程序博客网 时间:2024/04/29 22:24
比较运算: http://blog.csdn.net/ithomer/article/details/6836382
check the current user
#!/bin/bashtest=$(env | grep USER | cut -d "=" -f 2)if [ "$test" == "yu" ] then echo "the user is yu"fi
test if the disk is full
#!/bin/bashtest=$(df -h |grep sda2 | awk '{print $5}' |cut -d "%" -f 1)if [ "$test" -ge "90" ] then echo "is full"fi
test if is a directory
#!/bin/bashread -t 30 -p "enter a dir: " dirif [ -d "$dir" ]then echo "is dir"else echo "not dir"fi
test if Apache server is up
#!/bin/bashtest=$(ps -aux | grep httpd |grep -v httpd) #the script name should not contain "httpd"if [ -n "$test" ] then echo "httpd is started" >> correct.logelse echo "httpd not started" >> error.log /etc/rc.d/init.d/httpd startfi
make a calculator
#!/bin/bashread -t 30 -p "please input number 1: " num1read -t 30 -p "please input number 2: " num2read -t 30 -p "please input operator: " operif [ -n num1 -a -n num2 -a -n oper ] #all not nullthen test1=$(echo $num1 | sed 's/[0-9]//g') #substitute number to '' test2=$(echo $num2 | sed 's/[0-9]//g') if [ -z "$test1" -a -z "$test2" ] #quotes are needed, test all null then if [ "$oper" == "+" ] then result=$(($num1 + $num2)) #double () elif [ "$oper" == "-" ] then result=$(($num1 - $num2)) elif [ "$oper" == "*" ] then result=$(($num1 * $num2)) elif [ "$oper" == "/" ] then result=$(($num1 / $num2)) else echo "not valid operator" exit 10 fi else echo "not valid number" exit 11 fifiecho "result is: $result"
after execute the script, echo $? to check the exit code
test the file type
#!/bin/bashread -p "please input a file name: " fileif [ -z "$file" ]then echo "file name is null" exit 1elif [ ! -e "$file" ]then echo "file does not exist" exit 2elif [ -f "$file" ]then echo "file is a regular file"elif [ -d "$file" ]then echo "file is a directory"else echo "file is an other file"fi
case
#!/bin/bashread -p "input yes or no: " -t 30 chocase "$cho" in "yes") echo "choose is yes" ;; "no") echo "choose is no" ;; *) echo "choose is not yes or no" ;;esac
for loop
for parameter in value1 value2 value3 do procedure donefor i in 1 2 3 4 5 do echo $i done
#!/bin/bashcd /root/testls *.tar.gz > ls.logls *.tgz >> ls.logfor filename in $( cat ls.log ) # $() is used to call linux command, like ``do tar -zxf $filename &>/dev/nulldonerm -rf ls.log
#!/bin/bashs=0for ((i=1; i<=100; i=i+1))do s=$(($s+$i))doneecho "sum is: $s"
#!/bin/bashread -t 30 -p "user name: " nameread -t 30 -p "number of users: " numread -t 30 -p "password: " passif [ -n $name -a -n $num -a -n $pass ]then test_num=$(echo $num |sed 's/[0-9]//g') if [ -z "$test_num" ] then for ((i=1; i<$num; i=i+1)) do /usr/sbin/useradd $name$i &>/dev/null echo $pass | /usr/bin/passwd --stdin $name$i &>/dev/null done fifi
#!/bin/bashfor username in $(cat /etc/passwd |grep '/bin/bash' |grep -v root |grep -v yu |cut -d ":" -f 1)do userdel -r $usernamedone
#!/bin/bashi=0s=0while [ $i -le 100 ]do s=$(( $s + $i )) i=$(( $i + 1 ))doneecho "sum: $s"
#!/bin/bashi=0s=0until [ $i -gt 100 ]do s=$(( $s + $i )) i=$(( $i + 1 ))doneecho "sum: $s"
0 0
- shell脚本常用脚本
- 常用shell脚本积累
- 常用的shell脚本
- 常用Shell脚本集
- linux常用shell脚本
- shell 脚本 常用总结
- 常用shell脚本
- shell脚本----常用知识
- Linux shell常用脚本
- Linux Shell 常用脚本
- shell脚本--常用知识
- 常用的shell脚本
- 常用经典shell脚本
- 常用分析shell脚本
- Shell脚本常用符号
- Linux常用Shell脚本
- shell常用脚本比较
- shell脚本常用功能
- 转场动画(CATransition)的基础知识
- JdbcTemplate学习笔记
- adb 不是内部或者外部命令
- OpenGL教程翻译 第二十二课 使用Assimp加载模型
- 一、企业号相关操作一
- 常用 shell 脚本
- 微信公众号WebApp接管返回键实现“再按一次退出程序”功能
- iOS工程通用模块介绍 二
- 解决程序提示“应用程序发生异常 未知的软件异常(0x0eedfade),位置为 0x7c812fd3”
- UITabBarController(菜单栏)带导航栏的tabBar
- 如何评价 Swift 语言?
- iOS开发网络数据之AFNetworking使用
- HasMap中添加List<String>遍历输出结果不对
- Spring+Struts2+Hibernate三大框架整合常见问题解析