for---while
for 循环 seq 序列[root@up12 for-linux]# seq 512345[root@up12 for-linux]# seq 1 2 1013579[root@up12 for-linux]# echo {1..5}1 2 3 4 5[root@up12 for-linux]# echo {1..10..2}1 3 5 7 9[root@up12 for-linux]# echo {2..10..2}2 4 6 8 10
for i in 1..5; do echo "ok"donefor i in ‘seq 10’ ; do echo “ok”donefor的一种用法:
sum=0for i in {1..200} ; do sum=$sum+$1done echo $sum |bc //bc是shell里面自带的计算器for的第二种用法
[root@up12 for-linux]# for ((i=0 ;i<10 ;i++));do echo $i; done0123456789shell中的运算方法 ,其中 (()) 的运算速度是最快的
[root@up12 for-linux]# echo $[1+1]2[root@up12 for-linux]# echo $((1+1))2[root@up12 for-linux]# let a=1+1[root@up12 for-linux]# echo $a2[root@up12 for-linux]# expr 1+11+1[root@up12 for-linux]# expr 1 + 12[kate@up12 ~]$ declare -i b=2+3 //声明变量[kate@up12 ~]$ echo $b5[root@up12 for-linux]# bc bc 1.06.95Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.This is free software with ABSOLUTELY NO WARRANTY.For details type `warranty'. 1+12whilewhile cmd1;docmd2donei=0while [ “$i” -lt 10 ] ;do echo $i i=$((i+1)) let i++ done输出重定向,两句话的效果一模一样 >/dev/null 2>&1 &>/dev/null << here 文档 <<< here 字符串 basename /.../ dirname /.../
[kate@up12 ~]$ basename /opt/for-linux/for-linux[kate@up12 ~]$ dirname /opt/for-linux//opttee 会输出到屏幕,也会输入到文件 echo uplooking| passwd --stdin root //root 用户下,非交互的修改密码 =~只能在[[ ]]里面用,后面一定跟正则表达式
[kate@up12 ~]$ cut -d":" -f7 /etc/passwd |sort | uniq -c |sort -n |tail -1 | tr " "|cut -d " " -f6 //37[kate@up12 ~]$ cut -d":" -f7 /etc/passwd |sort | uniq -c |sort -n |tail -1 | tr -s " " | cut -d " " -f237文件的备份
#!/bin/bashsrcfile=/etc/hostsdstdir=/tmp/backupif [ ! -e "$srcfile" ];then echo "需要备份的文件不存在" exit 1fiif [ ! -d "$dstdir" ];then mkdir -p $dstdir if [ $? != 0 ];then echo "备份目录创建失败" exit 2 fifi#cp -a $srcfile $dstdir/$(basename $srcfile)-$(date +"%Y%m%d")cp -a $srcfile $dstdir/`basename $srcfile`-`date +"%Y%m%d"` //这里的引号为反引号if [ $? = 0 ];then echo "备份成功"else echo "备份失败"fi