【shell脚本学习-4】

文本处理

#!/bin/bash
#----------文本处理----------

#---------------echo-----------------
# "-n":处理光标在末尾
echo -n "Please Input Your Name"
read Name
echo -n "Please Input Your Sex"
read Sex

# "-e:转义"
echo -e "Name \t Sex \t" #\t:制表;\a:响铃
echo -e "Alice \t Women \t"
echo -e "Shell Script\a"

for((i=1;i<10;i++))
do
for((j=1;j<=$i;j++))
do
echo -n -e "$i*$j\t" #制表符
done
done

#-------------flod-----------
str=`fold -s -w 5 1.txt`
#文本处理fold{-s:折行;-w:折断的列数}
echo $str

#----------------fmt---------------

str=`fmt -c -w 3 1.txt`
#文本格式化fmt{-c:首行缩进;-w:保持字符完整性的折行}
echo $str

#-------------------rev--------
str=`rev 1.txt`
#反转文本
echo $str

#-------------pr------------------
str=`pr -h "Server Script Language" -a -s 1.txt`
#文本显示格式化pr{-h:标题}
echo $str

#-------------------sort-----------------

#sort文本排序
str=`sort -k 2,3 1.txt ` #-k{start,end}
echo -e "$str\n"

if [ $1 -gt 6 ] #接受用户输入的第一个形参进行条件测试
then
echo "Sorry,You Input Line Gather Than 6"
fi
result=`sort -r -k $1 1.txt` #逆序第一个形参
echo $result

str=`sort -n -k 80.1,85.1 1.txt` #列的字符
echo $str

str=`sort -t : ¥ -k3n,3 /etc/passwd`
echo $str

str=`sort -u /etc/sysconfig/network-scripts/ifcfg-ens33`
#排除重复列
echo -e $str \n > /etc/sysconfig/network-scripts/ifcfg-ens33

str=`sort -u -m 1.txt 2.txt > total.txt` #合并文本
echo $str

#文本行号
str=`nl -b a 1.txt > 1.txts`
echo $str

#--------------grep--------------------

echo -n "Please Input Your Name"
read Name
while [ $Name != "e" ]; do #当输入不为"e"继续统计输入的行数
Quiaty=`grep -c "$Name" 1.txt`
echo "$Name Line is $Quiaty"
echo -n "Please Agine Input Your Name "
read Name #再次读取用户输入
#statements
done
#--------------------cut------------------

#字符截取
str=`cut -d ":" -f 1,3 /etc/passwd` #分割,匹配列
echo -e "$str\n"

str=`cut c1-3 /etc/passwd` #1-3行
echo -e "$str\n"

cut -s -d ":" -f 2 /etc/passwd > 1.txt #筛选不重复
#more 1.txt

#---------------paste---------------

result=`paste -d "¥" 1.txt 2.txt > 3.txt` #拼接,"-d":分割符
echo -e "$result\n\t"
#-----------------------------join-----------

#!/bin/bash
:<<EOF
echo "a 12 a" > 1.txt
echo "b 53 B" > 2.txt
paste -d: "¥" 1.txt 2.txt > 3.txt #拼接1,2文本
cut -f1,3 3.txt > 3.txts #截取3文本1-3列
EOF

result=`join -1 1 -2 2 1.txt 2.txt > 3.txt` #不同文件的指定列拼接
echo -e "$result\n"

result=`join -a 1 -a 2 -o 1.1 2.1 1.txt 2.txt > 3.txt`
#左 -a 1|右 -a 2|全 -a 1 -a 2连接
#-o 文件自定义列
echo -e "$result\n"

#-------------------------------tr--------

result=`cat 1.txt | tr -s ["\n"]` #tr -s "[a-z]" < 1.txt`#筛选|空格不重复的字符
echo -e "$result\n"

for file in `ls /home`
do
echo $file | tr '[a-z]' '[A-Z]' #大小写转换
done

result=`tr -d '[0-9][:]' < 1.txt` #指定字符删除
echo $result

result=`tr -cs "[a-z][A-Z]" "[\n*]" < 1.txt` #补集添加字符删除重复机特殊字符
echo $result

#!/bin/bash
#!/bin/sed
#------------------------流编辑---------------

#替换文本
:<<FTP
result=`sed -n '1,3p' 1.txt > 2.txt` #筛选1-3行
echo -e "$result\n"
result=`sed 's/\(This\)\(is\)\(string\)/\1\3\2\/'` #调整字符显示顺序
# 's/a/A/g &/' 1.txt`
#'s;<[^>]*>;;g' 1.txt` #";"替代"/"
#'s/<[^>]*>//g' 1.txt` #筛选伪标签
#'2,4 s/a/A/g' 1.txt ` #> 2.txt #替换文本{g:全局;指定行号}
#'0~2 p' 1.txt > 2.txt` #筛选偶数行
#'1~2 p' 1.txt > 2.txt` #筛选奇数行
#'$,p' 1.txt > 2.txt #筛选首行
echo -e "$result\n"

#删除文本

result=`sed -e '0~2 d' 1.txt > 2.txt` #偶数行
#'$ d' 1.txt > 2.txt #末行
#'1 d' 1.txt > 2.txt` #首行
echo -e "$result\n"

#追加文本
result=`sed -e '2 a shell scripts' 1.txt > 2.txt`
echo -e "$result\n"

FTP

#插入文本
expr=`sed '$ i end string insert' 1.txt > 2.txt`
echo -e "$expr\n"

#=======================sed组合==================

result=`sed -n -e 's/a/A/g' -e '1,2 p' 1.txt` #替换+截取
echo -e "$result\n"

result_x=`sed -e 's/b/B/g;2 i Shell Script' 1.txt` #替换+插入(;独立执行)
echo -e "$result_x\n"

result_y=`sed -n 1,5'{
#替换+截取+插入
s/a/A/g
s/b/B/g
2 i Shell Script
p
}' 1.txt`
echo -e "$result_y\n"

#读取
result_z=`sed -n 1,5'{
f /etc/sysconfig/selinux
p
}' selinux_backup`
echo -e "$result_z\n"

#=============================awk=======================

#输出

Test_Var=`awk '{ print }' 1.txt` #基本输出{ print }
echo "$Test_Var"

Test_Var_One= `awk -f test.awk 1.txt` #文件读入输出
echo "$Test_Var_One"

#!/bin/awk -f #可执行文件输出
{ print }
#匹配

Test_Var_Tow=`awk '$2>100 { print } ' 1.txt` #$列数变量匹配
echo "$Test_Var_Tow"

Test_Var_There=`awk '/^(a|b)/ { print }' 1.txt` #匹配多个字母开头
#'/^a/ { print }' 1.txt` #匹配开头
echo "$Test_Var_There"

Test_Var_Four=`awk '/^a/ && $2 > 100 { print }' 1.txt`
echo "$Test_Var_Four"

#! /bin/awk -f

#函数
BEGIN{

#x="shell sciprt"
print index("shell script","script") #位置
print length("shell script") #长度
match("shell script",/o/) #正则匹配字符出现位置及长度
print RSTART ,RLENGTH
string="5a123B4"
split(string,arry,/[aB]/) #分割数组
print arry[0]
print arry[1]
print arry[2]
string="abc001dad0adadabc015"
sub(/(abc)+[0-9]*/,"(&)",string)
print string #选取特定字符赋予标记
rsub(/(abc)+[0-9]*/,"(&)",string)
print string
array[0]="a"
array[2]="b"
array[1]="c"
array[5]="r"
for ( i in array){ #遍历数组
print i
}

}

:<<EOF

{
grade=($2>100? "A":"B") #条件运算符
print $grade
}

{
print
print "===========" #记录分割符
#x="shell" "script" #awk变量
#print x
}
BEGIN{
RS="" #记录分割符
FS="\n" #字段分割符
}
{
print $1

}

EOF
:<<EOF
BEGIN { #开始模式
print "shell start"
}
{ print }
END { #结束模式
print "shell over"
}
EOF

:<<EOF
Test_Var_Four=`awk '/^a/ && $2 > 100 { print }' 1.txt` #混合模式
echo "$Test_Var_Four"

Test_Var_Five=`awk '/^a/ $2==21 { print } ' 1.txt` #区间模式
echo "$Test_Var_Five"
EOF

正则匹配

#!/bin/bash
#----------------RE基础正则表达式------
:<<FTP
str=`ls /bin | grep "^t"` #匹配开头
echo $str

end=`ls /bin | grep "g$"` #匹配结尾
echo $end

cd /home
touch a
touch a1
touch b2
single=`ls /home | grep "a."` #匹配任意一个指定字符
echo $single

str=`ls /bin | grep "^aaa*"` #匹配重复出现的字符
echo $str

str=`ls /bin | grep "^[^a-b]"` #"^[a-b]"` #匹配范围和范围之外
echo $str
cd /home
rm -rf *
touch a
touch aaa
str=`ls /bin | grep "a\{2,\3}"`
echo $str

echo "Shell Script" > test.sh
str=`cat ./test.sh | grep "/<S" #"t\>"` #匹配含有字符开头|结尾的字符串
echo $str

FTP
#-------------------ERE扩展正则表达式---
:<<cat
str=`ls /etc/ | egrep "^ss?"` #+" #匹配出现至少至多一次的字符
echo $str

str=`ls /bin/ | egrep "(ls | w | p$)"` #(),|匹配元素子集或
echo $str

touch a
touch b
touch c
str=`ls ./ | egrep "(a | c )"`
echo $str

#----------------Perl正则表达式------
str=`ls /etc/ | grep -P "rc/D"` #"rc\d"` #匹配|非0-9数字

echo $str

#字符集
str=`ls /bin | grep "[:dight:]"`
echo $str

#--------正则表达式应用--------

str=`cat /etc/aliases | grep "^a"`
echo $str

str=`grep "\." 1.txt` #匹配转义
echo $str
cat

#字符集匹配

str=`egrep "800-[[:digit:]]${4}-[[:digit"]]${3} 1.txt"`
echo $str

str=`egrep "[[:digit:]]{4}$" 1.txt` #^[[:digit:]]{3}" 1.txt`
echo $str
#筛选3个数字开头|结尾

str=`egrep "^([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}$" 1.txt`
echo $str
#正则运算优先级和子运算符

str=`ls /bin/[a-b]*` #通配符
echo $str

函数

#!/bin/bash

#------------------------函数--------------------------------

function Test_Function(){ #定义函数法一
echo "这是一个测试函数"
}
Test_Function #调用函数

Test_Tow_Function(){
echo "这是另一种测试函数"
}
Test_Tow_Function

function GetCureentTime(){
Cureent_Time=`date` #获取当前时间
echo "$Cureent_Time"
}
#必须在函数定义后的位置调用这个函数
GetCureentTime

function SonLinkOneFunction(){
echo "这是第一个子链接函数"
}
function SonLinkTowFunction(){
echo "这是第二个子链接函数"
}
function ParentLinkFunction(){
SonLinkOneFunction #在父函数内调用定义的子链接函数
SonLinkTowFunction
}

Sum(){
let "z=$1 + $2"
return "$z" #函数返回值
}
Sum 10 40 #调用sum函数
echo "$?" #返回状态码

#接受自定义字符
echo "请输入你希望计算的字符"
read x
str=$1
result=0
Length(){
#计算这个字符的长度
if [ "str" != "" ];then #判断字符不为空,不算长度
result=${#str}
#statements
fi
echo "$result"
}
Char_Test=$(Length $x) #调用这个长度函数,代入自定义参数
echo "这个字符的长度是 $Char_Test"

function AliasFunction(){
Alias ls -Z='ls' #别名
}
AliasFunction
unset AliasFunction #删除函数

Out_Var="This is Function Out Var"
function VarFunction(){
Out_Var="Change This Out_Var"
echo "$Out_Var" #在函数内调用经函数改变的变量
local In_var="This is Function In Var"
#定义局部变量,全局调用是将无输出结果
}
VarFunction
echo "$In_var" #在函数外调用函数内的变量

function ChanSuoFcuntion(){
echo "The Cureent ChanSuo length is $#"
echo "The Cureent ChanSuo Sum is $@"
echo "The Cureent ChanSuo name is $0"
}
#ChanSuoFcuntion a b c d #赋予参数,计算个数
ChanSuoFcuntion "Shell Script"

#ChanSuoFcuntion a b c d #赋予参数,计算个数
ChanSuoFcuntion "Shell Script"

function MoveFunction(){
while(( $# > 0))
do
echo "$1"
shift #把参数向前移动一个单位长度
done

}
MoveFunction

#!/bin/bash

function ChanSuoFcuntion(){
echo "The Cureent ChanSuo length is $#"
echo "The Cureent ChanSuo Sum is $@"
echo "The Cureent ChanSuo name is $0"
#参数的位置
}
#ChanSuoFcuntion a b c d #赋予参数,计算个数
ChanSuoFcuntion "Shell Script"

function MoveFunction(){
while(( $# > 0))
do
echo "$1"
shift #把参数向前移动一个单位长度
done

}
MoveFunction

function TestFucntion(){
while getopts "a:b:c" arg #接受参数保存在$OPTAGR中
do
case "$arg" in
a)
echo "a'$OPTARG";;
b)
echo "b'$OPTARG";;
c)
echo "c";;
?)
echo "Unkown"
exit 1;;
esac

done
}
TestFucntion -a "shell" -b "script"

function TestTowFucntion(){
echo "$1"
}
name=shell
type=name
TestTowFucntion $type #调用变量
TestTowFucntion ${!name} #间接调用变量

file=/bin/ls
function HaveFunction(){
if [[ -e $file ]]; then
echo "The file have"
else
echo "The file not have"
#statements
fi
}
HaveFunction
file=/bin/a #修改函数条件测试值
HaveFunction

function ArrayFunction(){
echo "The number is $#"
while [[ $# -gt 0 ]]
do
echo "$1"
shift
done
}
a=(a,b,"cd",e)
ArrayFunction "${a[@]}" #获取数组参数值

. lib.sh #载入库文件
mesg="This is a file"
Erro "$mesg" #调用库文件函数

function DiGuiFunction(){
read y
DiGuiFunction "$y" #递归函数
echo "$y"
}
DiGuiFunction

function Factor(){
local n=$1; #定义一个本地变量
if [ $n -eq 0 ]
then #计算n等于1时的阶乘
result=1
else #不等于1的1阶乘
let "m=n-1"
fact "$m"
let "result=$n*$?"
fi
return "$result"

}
Factor $1
echo "$1 DiGui is $?"

逻辑语句

#条件语句

#---------------------------if------------------------------------

#!/bin/bash#嵌套双分支

echo "input your socre:"

read socre

if [ -z "$score" ]; then

echo "your score is null,please input aegin"

else

if  [ "$score" -lt  100  -a "$score"  -gt 0];then

echo "your score is right,you can continue excute"

else

if [ "$score" = 90 ];then

echo "Your score level is A"

else

if  [ "$score"=70];then

echo "Your score level is B"

if [ "$score"=60];

echo "Your score level is C"

else

echo "Your score level is D"

fi

fi

fi

#statements

fi

echo "please input your score"#多分支

read  score

if [ -z "$score" ];then#判断分数是否为空值

echo "your score is null,please input aegin"

else

if [ "$z" -ge 100  -a    "$z" -le  0 ];then#判断分数是否在[0,100]之间

echo "your score not between 0 and 100,please agin"

read score   #重新读取用户输入

else

if[ "$z" -le 60 ]; then

echo "your score is D"

elif[ "$z" -le 70 ];then

echo  "your score is C"

elif[ "$z" -le 80 ];then

echo "your score is B"

else

echo "your score is A"

fi

fi

fi

if [ -e "$1" ];then#判断文件是否存在

echo "$1 file have"

exit 1#文件不存在状态码

else

touch "$1"

echo "$1 file created"#不存在创建

exit 0#文件存在状态码

fi

#----------------------------------for---------------------

#!/bin/bash

for (( i = 0; i < 10; i++ ));do

if [[ "$i%2" -eq 0  ]];then

continue#跳出这个输出

#statements

fi

echo "$i"#输出所有奇数

#statements

done

for i in   a b c d

do

echo -n "$i"

for j in `seq 10`

do

if [ $j -eq 5 ]; then

#break 2#退出状态码

continue   2 #跳出状态码

#statements

fi

echo -n "$j"

done

echo

#statements

done

#!/bin/bash

for((i=0;i<10;i++));do  #嵌套循环

for (( j=0;j<i;j++ )); do

let "product=i*j"

printf "$i*$j=$product"

if [[ $product -gt 10 ]]; then

echo "   "

else

echo "    "

#statements

fi

if [[ $product -ge 5 ]]; then#内层循环条件中断

break 2

#statements

fi

#statements

done

echo

done

if [[ $product -gt 5 ]]#外层循环条件中断

then

break#中断这部分的输出

fi

#for i in `ls .`#for..in循环

#do

#if echo "$i" | grep "a"

#then

#echo "$i"

#fi

#done

for i in  [1..10]    #1 2 3 5 8

do

echo "The Cureent number is $i"

done

total=0#for循环

for i in  {1..100..2}    #1 2 3 5 8#{start,end,步长}

do

let "total=total+i"

#echo "The Cureent number is $i"

done

echo "total is $total"

for  days in {M T S T F S S}#列表值

do

echo "Today is $days"

done

for x in    $(ls)   #*  #"ls" #循环显示命令

do

echo "$x"

done

echo "$*"#接受输入所有的参数

for arg in "$*"

do

echo "${arg}"

done

for arg in#不带列表的for

do

echo "${arg}"

done

x="a"

for ((;;)) #类c的死循环  #((i=0;i<=5;i++))#类c的for

do

echo "$x"

done

array=(M,T,T,F,S,S)#处理数组

for i in ${array[*]}

do

echo "$i"

done

#---------------------------while--------------------------

wile getopt ":pq:"y:#while循环

do

case $y in

"p")

echo "$y";;

"q")

echo "$x $y";;

"::")

echo "$x";;

"?")

echo "$x

"*")

echo "z";;";;

esac

echo "w"

done

while [[ i=0;i<9;i++ ]]; do

while [[ j=0;j<i;i++ ]]; do

if [[ j -gt 9 ]]; then

let "product=i*j"

printf "$i*$j=$product"

#statements

else

echo "   "

echo "    "

fi

#statements

done

echo

#statements

done

echo "please input your number,must between 1 and 10,and 0 exits"

read number

while [[ "$number" != 0 ]]; do   #while判断计数

#echo "you can input"

#read number#再次读取用户输入

if [ "$number" -lt 5 ];then#多分支判断准确输入

echo "sorry,your number lt 5"

read number

elif [ "$number" -gt 5 ];then

echo "sorry,your number gt 5"

read number

else

echo "Configuration,You are right!"

exit 0#退出

fi

#statements

#statements

done

while [[ "$i" -lt 10 ]]; do  #while循环

let "square=i*i"#let条件计算

echo "$i*$i=$square"

let  "i++"#自增计数器

#statements

done

#------------------------util-----------------------

i=1

until [[ "$i"  -gt 10 ]]; do

#开始until循环,条件测试i>10时退出until循环

let "square=i*i"#let条件计算

echo "$i*$i=$square"

let  "i++"#自增计数器

#statements

done#中断

i=1

until [[ "$i" -ge 21 ]];do

userdel user$i#代入计数器i,执行创建用户

#echo "123"  |  passwd --stdin user$i >> /dev/null   #设置密码

let "i++"#自增

#statements

done

i=1

until [[ "$i"  -gt 10 ]]; do  #开始until循环,当i>10时退出until循环

let "square=i*i"#条件

echo "$i*$i=$square"

let  "i++"#自增计数器

#statements

done

#-------------------------case-----------------------------

echo "please input your char"

read char

case $char in      #接受变量

[[:upper:]])#判断条件1

echo "your char is letter";;#结束

[[:lower:]])

echo "your char is lower";;

[0-9])

echo "your char is number";;

*)#默认

echo "your char is other";;

esac#终止循环$

数组

#----------数组--------

#!/bin/bash

array[0]="shell"#数组标准定义

array[2]="script"

echo "${array[@]}"#数组输出

declare -a x#修饰数组

x[0]="shell"

x[3]="script"

echo "${x[@]}"

array=(1,2,3,4,5)#集合定义数组

echo "The array firset  is ${#array[0]}"

echo "The array is ${array[@]}"

echo "The array is length ${#array[@]}"

array=([0]="a",[1]="b",[3]="c")#键值对定义数组

echo "The array is ${array[@]}"

array="shell script" #数组和普通变量

echo "${array[*]}"

echo "${array[@]}"

Students=(Jark Tindy Marks Roses)

echo "Students have ${Students[*]}"

declare -A Grandes

Grandes=([jark]=50 [tindy]=90 [marks]=69 [roses]=30)

echo "Jark Grandes is ${Grandes[jark]}"

Students[jark]=90#通过索引修改关联数组元素值

echo "Jark Grandes is ${Grandes[jark]}"

x=(a b c)

echo "${x[@]}"

x=(a b)#数组值的覆盖

echo "${x[*]}"

array=( 1 2 )

echo "${array[@]}"

array[2]=3#向数组末尾追加元素

echo "${array[@]}"

declare -A array    #管理数组追加元素

array=([a]=1 [b]=2)

echo "${array[@]}"

array[c]=3

echo "${array[@]}"

for i in {1..10}#for为数组赋值

do

array[$i]=$i

done

echo "${array[*]}"

array=(array[0]="Python" array[1]="Javascript" )

for i in {1..2}#循环输出数组

do

echo "${array[$i]}"

done

x=("a" "b" "c")

echo "${x}"#访问数组默认下表0

echo "${x[1]}"#逐个访问

array=("a" "b" "c" "d" "e" "f")

len="${#array[@]}"#获取数组长度

for((i=0;i<=$len;i++)){

echo "${array[$i]}"

}

array=("a" "b" "c")

for e in "${array[@]}"#数组所有元素作为for条件

do

echo "$e"

done

array=("A" "B"  "C")

echo "${array[*]:1:1}"#数组切片start::step

x=(1 2 3)

x=("${x[@]/2/20}")#数组元素的替换

echo "$x"

echo "${x[*]/1/100}"

y=("a" "b" "c")

echo "Old Legth  is ${#y[@]}"

unset y[1]#删除数组元素

echo "New Legth is ${#y[@]}"

y2=("${y[@]}")#复制数组

echo "Copy is ${y2[*]}"

#unset y#删除全部数组

#echo  "${y[@]}"

x=("Python" "Javascript" "Php" )

y=("Micrsoft" "Mac" "Linux")

z=("${x[@]}" "${y[@]}")#连接数组

echo "${z[@]}"

echo "1 2 3" > ./1#文本导入数组

content=(`cat 1`)

echo "${content[*]}"

运算符及表达式和条件测试

#-----------------------------运算符及表达式和条件测试----------------

#!/bin/bash

#source,sh,./shell_name :shell脚本执行方法

#

#变量

#declare :修饰

x=10/2

echo "$x"

#将变量修饰为只读

declare  -r x

x="a"#再次为变量赋值检验修饰

echo "$x"

|

#将变量修饰为只为整数

declare -i  x

x=10

echo "$x"

#typeset <=> declare

y=8/2

echo "$y"

typeset-i y

y="b"

echo "$y"

#x=1;

#w=9;

#let "x+1"

#echo "$x"

#y="$(x+w)";

#z="a";

#echo "$x $y $z";

:<<cat

Alias   rm="rm -i"

#./home/user_name,/etc/profile,/etc/bashrc用户shell的配置文件

more shells#shell的种类

echo "$SHELL    '当前shell的版本是'  $BASH_VERSION" #打印当前使用shell类型

echo "hello world"

echo "$?"#shell退出状态码<0-255>

abc

echo "$?"

#exit 120

#echo  "$?"

cat

:<<ftp  #多行注释

echo  "$#"     #返回传入参数的个数      #单行注释

echo  "$@"

echo  "$0"   #返回当前脚本的名称

echo  "$_"

echo  "$*"    #分别列出参数

#!/bin/bash

#关于引号 "" ,'',``

test [ 1 -eq 2]

echo "$$"#进程pid

echo "$0"#脚本名称

echo "$1\=$1$2\=$2$4\=$4"

#环境变量

echo "$pwd"

echo "$LOGINAME"

set | more#显示可建立的全部环境变量

x=123

unset $x#清除变量

echo $x

cd /bin

ls t*#通配开头

x=`pwd`

echo "cureent directory is $x"

echo 'cureent directory is $x'

#echo ${x}abc#"{}变量分界符"

:<<FTP

echo "开始你的程序"

read  "$z"

echo "当前所在的目录是 `pwd` and 你主机的地址是 `ifconfig`"

echo "程序结束"

result=`expr  3 / 2`

echo "$result"#expr:算数计算符

#result=`expr  \(1 * 2\)\ + 1`

#echo "$result"

result=$(( 10 + 90  / 2 ))#$(()):算数运算符

echo  "$result"

#$[]算数运算符

result=$[ 5*2 ]

echo "$result"

n=1#let算数运算符

let n=n+1

echo "$n"

result=`expr 4 +=  6`

echo "$result"

#!/bin/bash

result=$((4 += 6))#复合算数运算符

echo "$result"

:<<FTP#位运算符

x=let "2 >>  1 "#2的二进制向右移一位

echo "$x"

y=$[ "$x" ~= 1 ]#变量x与1异或

echo "y"

z=`expr 1 ^ 0`#1和0取反

echo $z

FTP

result=$[ 2 << 1 ]

echo "$result"

x=1

result=$[ x |= 2 ]#位复合运算符

echo "$result"

y=5#自增自减运算符

result=$[ y+(++y) ]

echo "$result"

result=$((result++))

echo "$result"

result=$[ --result ]

echo "$result"

#法一:

((x=201))#二,八,十六进制

echo "$x"

((y=021))

echo "$y"

((z=0x21))

echo "$z"

#法二

((x=2#201))

echo "$x"

((y=8#201))

echo "$y"

((z=16#201))

echo "$z"

#条件语句#---------------------------if------------------------------------
#!/bin/bash#嵌套双分支 echo "input your socre:"read socreif [ -z "$score" ]; thenecho "your score is null,please input aegin"elseif  [ "$score" -lt  100  -a "$score"  -gt 0];thenecho "your score is right,you can continue excute"elseif [ "$score" = 90 ];thenecho "Your score level is A"elseif  [ "$score"=70];thenecho "Your score level is B"if [ "$score"=60];echo "Your score level is C"elseecho "Your score level is D"
fififi#statementsfiecho "please input your score"#多分支read  scoreif [ -z "$score" ];then#判断分数是否为空值echo "your score is null,please input aegin"elseif [ "$z" -ge 100  -a    "$z" -le  0 ];then#判断分数是否在[0,100]之间echo "your score not between 0 and 100,please agin"read score   #重新读取用户输入elseif[ "$z" -le 60 ]; thenecho "your score is D"elif[ "$z" -le 70 ];thenecho  "your score is C"elif[ "$z" -le 80 ];thenecho "your score is B"elseecho "your score is A"fi
fifiif [ -e "$1" ];then#判断文件是否存在echo "$1 file have"exit 1#文件不存在状态码else touch "$1"echo "$1 file created"#不存在创建exit 0#文件存在状态码fi

#----------------------------------for---------------------
#!/bin/bashfor (( i = 0; i < 10; i++ ));doif [[ "$i%2" -eq 0  ]];thencontinue#跳出这个输出#statementsfiecho "$i"#输出所有奇数#statementsdone
for i in   a b c d doecho -n "$i"for j in `seq 10` doif [ $j -eq 5 ]; then#break 2#退出状态码continue   2 #跳出状态码#statementsfiecho -n "$j"doneecho #statementsdone

#!/bin/bashfor((i=0;i<10;i++));do  #嵌套循环for (( j=0;j<i;j++ )); dolet "product=i*j"printf "$i*$j=$product"if [[ $product -gt 10 ]]; thenecho "   "elseecho "    "#statementsfiif [[ $product -ge 5 ]]; then#内层循环条件中断break 2 #statementsfi#statementsdoneecho done
if [[ $product -gt 5 ]]#外层循环条件中断thenbreak#中断这部分的输出fi

#for i in `ls .`#for..in循环#do#if echo "$i" | grep "a"#then#echo "$i"#fi#done
for i in  [1..10]    #1 2 3 5 8doecho "The Cureent number is $i"done

total=0#for循环for i in  {1..100..2}    #1 2 3 5 8#{start,end,步长}dolet "total=total+i"#echo "The Cureent number is $i"doneecho "total is $total"
for  days in {M T S T F S S}#列表值do echo "Today is $days"done
for x in    $(ls)   #*  #"ls" #循环显示命令doecho "$x"done
echo "$*"#接受输入所有的参数for arg in "$*"doecho "${arg}"done
for arg in#不带列表的fordo echo "${arg}"done
x="a"for ((;;)) #类c的死循环  #((i=0;i<=5;i++))#类c的fordoecho "$x"done

array=(M,T,T,F,S,S)#处理数组for i in ${array[*]}doecho "$i"done

#---------------------------while--------------------------

wile getopt ":pq:"y:#while循环docase $y in"p")echo "$y";;"q")echo "$x $y";;"::")echo "$x";;"?")echo "$x"*")echo "z";;";;esacecho "w"done

while [[ i=0;i<9;i++ ]]; dowhile [[ j=0;j<i;i++ ]]; doif [[ j -gt 9 ]]; thenlet "product=i*j"printf "$i*$j=$product"#statementselseecho "   "echo "    "fi#statementsdoneecho#statementsdone

echo "please input your number,must between 1 and 10,and 0 exits"read numberwhile [[ "$number" != 0 ]]; do   #while判断计数#echo "you can input"#read number#再次读取用户输入if [ "$number" -lt 5 ];then#多分支判断准确输入echo "sorry,your number lt 5"read numberelif [ "$number" -gt 5 ];thenecho "sorry,your number gt 5"read numberelseecho "Configuration,You are right!"exit 0#退出fi#statements#statementsdone
while [[ "$i" -lt 10 ]]; do  #while循环let "square=i*i"#let条件计算echo "$i*$i=$square"let  "i++"#自增计数器#statementsdone
#------------------------util-----------------------
i=1until [[ "$i"  -gt 10 ]]; do  #开始until循环,条件测试i>10时退出until循环let "square=i*i"#let条件计算echo "$i*$i=$square"let  "i++"#自增计数器#statementsdone#中断

i=1until [[ "$i" -ge 21 ]];douserdel user$i#代入计数器i,执行创建用户#echo "123"  |  passwd --stdin user$i >> /dev/null   #设置密码let "i++"#自增#statementsdone

i=1until [[ "$i"  -gt 10 ]]; do  #开始until循环,当i>10时退出until循环let "square=i*i"#条件echo "$i*$i=$square"let  "i++"#自增计数器#statementsdone

#-------------------------case-----------------------------

echo "please input your char"read char                    case $char in      #接受变量[[:upper:]])#判断条件1echo "your char is letter";;#结束[[:lower:]])echo "your char is lower";;[0-9])echo "your char is number";;*)#默认echo "your char is other";;esac#终止循环$

上一篇:22 Best Sites To Download Free Sprites


下一篇:38.IntelliJ IDEA中创建Web聚合项目(Maven多模块项目)