(6-2)shell编程 (2)

来源:互联网 发布:百度作业帮优化答案 编辑:程序博客网 时间:2024/06/05 18:04


for循环:通过使用一个变量去遍历给定列表中的每个元素,在每次变量赋值时执行一次循环体,直至赋值完成所有元素退出循环。-----------------------------------------格式 1 -----------------------------:写法一:[root@baolibin shell]# vi for1.sh[root@baolibin shell]# more for1.sh#!/bin/bash#----------for ((i=0;i<10;i++))doecho $idone[root@baolibin shell]# chmod +x for1.sh[root@baolibin shell]# ./for1.sh0123456789[root@baolibin shell]#写法二:[root@baolibin shell]# cp for1.sh for2.sh[root@baolibin shell]# vi for2.sh[root@baolibin shell]# more for2.sh#!/bin/bash#----------for ((i=0;i<10;i++));doecho $idone[root@baolibin shell]# ./for2.sh0123456789[root@baolibin shell]#-----------------------------------------格式 2 -----------------------------:[root@baolibin shell]# cp for1.sh for3.sh[root@baolibin shell]# vi for3.sh[root@baolibin shell]# more for3.sh#!/bin/bash#----------for i in 0 1 2 3 4 5 6 7 8 9doecho $idone[root@baolibin shell]# ./for3.sh0123456789[root@baolibin shell]#-----------------------------------------格式 3 -----------------------------:[root@baolibin shell]# cp for1.sh for4.sh[root@baolibin shell]# vi for4.sh[root@baolibin shell]# more for4.sh#!/bin/bash#----------for i in {0..9}doecho $idone[root@baolibin shell]# ./for4.sh0123456789[root@baolibin shell]#条件测试:bash条件测试:命令执行成功与否即为测试。第一种:test EXPR第二种:[ EXPR ]    注意:中括号与表达式之间有空格。整型测试:-gt: 大于,如 [ $num1 -gt $num2 ]-lt:小于-ge:大于等于-le:小于等于-eq:等于-nq:不等于字符串测试:>:大于  [ "$str1" \> "$str2" ]<:小于=!=算数运算:let varName=算数表达式varName=$[算数表达式]varName=$((算数表达式))varName=` expr $num1 + $num2 `[root@baolibin shell]# num=1+1[root@baolibin shell]# echo $num1+1[root@baolibin shell]# let num=1+1[root@baolibin shell]# echo $num2[root@baolibin shell]# num=$[1+2][root@baolibin shell]# echo $num3[root@baolibin shell]# num=$((1+3))[root@baolibin shell]# echo $num4[root@baolibin shell]# num=` expr 1 + 4 `[root@baolibin shell]# echo $num5[root@baolibin shell]# num = 1 + 1-bash: num: command not found[root@baolibin shell]#区分大小写:[root@baolibin shell]# touch TEST.sh[root@baolibin shell]# ll总用量 32-rwxr-xr-x. 1 root root 52 4月  18 21:58 test.sh-rw-r--r--. 1 root root  0 4月  19 13:40 TEST.shwhile/until循环适用于循环次数未知,或不便用for直接生成较大的列表时格式:while 测试条件;do循环体done如果测试条件为真,则进入循环,退出条件为测试条件为假until循环的格式和while循环的格式一致,但是与while循环的意思相反,如果测试条件为假则进入循环,测试条件为真时则退出循环。-------------------------------------while格式1----------------------------------------[root@baolibin shell]# vi while1.sh[root@baolibin shell]# more while1.sh#!/bin/bash#----------i=0while test $i -lt 10doecho $ilet i++done[root@baolibin shell]# chmod +x while1.sh[root@baolibin shell]# ./while1.sh0123456789[root@baolibin shell]#-------------------------------------while格式2----------------------------------------[root@baolibin shell]# cp while1.sh while2.sh[root@baolibin shell]# vi while2.sh[root@baolibin shell]# more while2.sh#!/bin/bash#----------i=0while [ $i -lt 10 ]doecho $ilet i++done[root@baolibin shell]# ./while2.sh0123456789[root@baolibin shell]#-------------------------------------until格式----------------------------------------[root@baolibin shell]# cp while1.sh until.sh[root@baolibin shell]# vi until.sh[root@baolibin shell]# more until.sh#!/bin/bash#----------i=0until [ $i -gt 10 ]doecho $ilet i++done[root@baolibin shell]# ./until.sh012345678910[root@baolibin shell]#if判断:单分支:if 测试条件;then选择分支fi双分支:if 测试条件then 选择分支1else选择分支2fi多分支:if 条件1;then分支1elif 条件2;then分支2elif 条件3;then分支3...else分支nfi单分支:[root@baolibin shell]# vi if1.sh[root@baolibin shell]# more if1.sh#!/bin/bash#-----------if [ $1 -eq 1 ]thenecho "one"fi[root@baolibin shell]# chmod +x if1.sh[root@baolibin shell]# ./if1.sh 1one[root@baolibin shell]# ./if1.sh 2[root@baolibin shell]#双分支:[root@baolibin shell]# cp if1.sh if2.sh[root@baolibin shell]# vi if2.sh[root@baolibin shell]# ./if2.sh 1one[root@baolibin shell]# ./if2.sh 2none[root@baolibin shell]#多分支:[root@baolibin shell]# cp if2.sh if3.sh[root@baolibin shell]# vi if3.sh[root@baolibin shell]# more if3.sh#!/bin/bash#-----------if [ $1 -eq 1 ]thenecho "one"elif [ $1 -eq 2 ]thenecho "two"elif [ $1 -eq 3 ]thenecho "three"elseecho "none"fi[root@baolibin shell]# ./if3.sh 1one[root@baolibin shell]# ./if3.sh 2two[root@baolibin shell]# ./if3.sh 3three[root@baolibin shell]# ./if3.sh 4none[root@baolibin shell]#case判断:有多个测试条件时,case语句会使语法结构更清晰格式:case 变量引用 in     PATTERN1)分支1;;     PATTER2)分支2;;     ...     *)分支n;;esacPATTERN:类同于文件名通配机制,但支持使用 | 表示或者a|b:a或者b*:匹配任意长度的任意字符?:匹配任意单个字符[]:指定范围内的单个字符[root@baolibin shell]# vi case.sh[root@baolibin shell]# more case.sh#!/bin/bash#----------case $1 in1)echo "one";;2)echo 'two';;3)echo "three";;*)echo "none";;esac[root@baolibin shell]# chmod +x case.sh[root@baolibin shell]# ./case.sh 1one[root@baolibin shell]# ./case.sh 2two[root@baolibin shell]# ./case.sh 3three[root@baolibin shell]# ./case.sh 4none[root@baolibin shell]#自定义函数function 函数名(){...}引用自定义函数文件时,使用source func1.sh有利于代码的重用性。[root@baolibin shell]# vi func1.sh[root@baolibin shell]# more func1.sh#!/bin/bash#------------function test(){echo 'this is a function'}test[root@baolibin shell]# chmod +x func1.sh[root@baolibin shell]# ./func1.shthis is a function[root@baolibin shell]#[root@baolibin shell]# cp func1.sh func2.sh[root@baolibin shell]# vi func2.sh[root@baolibin shell]# more func2.sh#!/bin/bash#------------source /usr/local/shell/func1.shtest[root@baolibin shell]# ./func2.shthis is a functionthis is a function[root@baolibin shell]#date:显示当前时间格式化输出: +%Y-%m-%d格式化%s:表示自1970-01-01 00:00:00以来的描述指定时间输出:--date='2009-01-01 11:11:11'指定时间输出:--date='3 days ago'[root@baolibin shell]# date2015年 04月 19日 星期日 14:38:40 CST[root@baolibin shell]# date +%Y-%m-%d2015-04-19[root@baolibin shell]# date +%Y-%m-%d %H:%M:%Sdate: 额外的操作数 "%H:%M:%S"请尝试执行"date --help"来获取更多信息。[root@baolibin shell]# date +%Y-%m-%d_%H:%M:%S2015-04-19_14:41:03[root@baolibin shell]# date "+%Y-%m-%d %H:%M:%S"2015-04-19 14:41:15[root@baolibin shell]#[root@baolibin shell]# date +%s1429425733[root@baolibin shell]# date --date="2015-01-05"2015年 01月 05日 星期一 00:00:00 CST[root@baolibin shell]# date --date="2015-01-05" +%Y-%m-%d2015-01-05[root@baolibin shell]# date +%s1429425804[root@baolibin shell]# date --date="2015-01-05" +%s1420387200[root@baolibin shell]#内建命令与外部命令:[root@baolibin shell]# date2015年 04月 19日 星期日 14:38:40 CST[root@baolibin shell]# type datedate is hashed (/bin/date)[root@baolibin shell]# type cdcd is a shell builtin[root@baolibin shell]# man dateread:read命令接收标准输入(键盘)的输入,或者其它文件描述符的输入。得到输入后,read命令将数据放入一个标准变量中。格式:read VAR_NAMEread -p "Enter your name:"VAR_NAMEread如果后面不指定变量,那么read命令会将接收的数据放置在环境变量REPLY中。read -t 5 -p "Enter your name:"VAR_NAMEread -s -p "Enter your password:"pass[root@baolibin shell]# echo $baozi[root@baolibin shell]# read baoziwww.hadoop.com[root@baolibin shell]# echo $baoziwww.hadoop.com[root@baolibin shell]# readhahahehe[root@baolibin shell]# echo $REPLYhahahehe[root@baolibin shell]# read -p "Enter your name:" baoziEnter your name:xiaobaoziya[root@baolibin shell]# echo $baozixiaobaoziya[root@baolibin shell]# read -t 3 -p "Enter your name:" baoziEnter your name:[root@baolibin shell]#[root@baolibin shell]# read -s -p "Enter your name:" baoziEnter your name:[root@baolibin shell]#[root@baolibin shell]# echo $baozihehe[root@baolibin shell]#declare:用来限定变量的属性:-r:只读-i:整数:某些算数计算允许在被声明为整数的变量中完成,而不需要特别使用expr或let来完成。-a:[root@baolibin shell]# vi declare1.sh[root@baolibin shell]# more declare1.sh#!/bin/bash#-----------num=0echo $numdeclare -r numnum=1echo $num[root@baolibin shell]# chmod +x declare1.sh[root@baolibin shell]# ./declare1.sh0./declare1.sh: line 6: num: readonly variable0[root@baolibin shell]#[root@baolibin shell]# cp declare1.sh declare2.sh[root@baolibin shell]# vi declare2.sh[root@baolibin shell]# more declare2.sh#!/bin/bash#-----------declare -i numnum=1+1echo $num[root@baolibin shell]# ./declare2.sh2[root@baolibin shell]#


0 0
原创粉丝点击