系统变量:
/etc/profile: 会话时读取一次
/etc/bashrc: 每次打开终端读取
/etc/profile.d:为不同版本shell特殊用法说明
用户变量
.bash_profile: 会话时读取一次
.bashrc: 每次打开终端读取
.bash_history: 记录用户以前输入的命令
.bash_logout: 用户退出是要执行的命令
用户变量
定义:me
赋值:me=jingzhao
显示:echo $me/read me
删除:unset me
只读:readonly me
转化成环境变量:export me
显示环境变量:env/export/set
判断语句:
第一种方式:[]/test/&&/||
[]: 进行数据判断
判断条件:
1.每个组件需要用空格来分隔;
2.变量用双引号来设置;
3.常量用单引号或者双引号来设置
例子:
[ -z "$HOME" ]
[ "$HOME" == "$MAIL" ]
test: 进行数据判断
例子:test$HOME=$MAIL
&&: 前面的命令执行结果为正确执行后面的命令;否则就略过;
例子:ls /tmp && touch /tmp/testingagain
||: 前面的命令执行结果为错误执行后面的命令;否则就略过
例子:ls /tmp && touch /tmp/testingagain
&&和||一起用:command1&&command2||command3
例子:ls /tmp/vbirding && echo "exist"||echo "not exist"
复合使用例子:
#!/bin/bash
#Program
# this program will show the use's choice
#History:
#2010/12/24 Tom first release
PATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/bin
export PATH
read -p "Please input (Y/N): "yn
[ "$yn" == "Y" -o "$yn" == "y" ]&& echo "OK, continue" && exit 0
[ "$yn" == "N" -o "$yn" == "n" ]&& echo "Oh, interrupt!" && exit 0
echo "I don't know what is your choise" && exit 0
第二种方式:if...then
结构一:
if[条件判断表达式]; then
当判断判断表达式,执行语句
fi
例子:
#!/bin/bash
#Program
# this program will show the use's choice
#History:
#2010/12/24 Tom first release
PATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/bin
export PATH
read -p "Please input (Y/N): "yn
if [ "$yn" == "Y" ]||[ "$yn" == "y" ]; then
echo "OK, continue"
exit 0
fi
if [ "$yn" == "N" ]||[ "$yn" == "n" ]; then
echo "Oh, interrupt!"
exit 0
fi
echo "I don't know what is your choise" && exit 0
结构二:
if[条件判断表达式1]; then
当判断表达式1成立,执行语句
elif[条件判断表达式2]; then
当判断表达式2成立,执行语句
else
当判断表达式1,2均不成立,执行语句
fi
例子:
#!/bin/bash
#Program
# this program will show the use's choice
#History:
#2010/12/24 Tom first release
PATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/bin
export PATH
read -p "Please input (Y/N): "yn
if [ "$yn" == "Y" ]||[ "$yn" == "y" ]; then
echo "OK, continue"
elif [ "$yn" == "N" ]||[ "$yn" == "n" ]; then
echo "Oh, interrupt!"
else
echo "I don't know what is your choise"
fi
第三种方式:case...esac
结构:
case $变量名称 in
"第一个变量内容")
符合第一个变量情况下程序段
;;
"第二个变量内容")
符合第二个变量情况下程序段
;;
*)
第一,二个变量内容均不符合情况下程序段
exit 1
;;
esac
例子:
#!/bin/bash
#Program
# this program will show the use's choice
#History:
#2010/12/24 Tom first release
PATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/bin
export PATH
echo "This program will show the user's choice!"
#read -p "Input your choice:" choice
#case $choice in
case $1 in
"one")
echo "Your choice is ONE"
;;
"two")
echo "Your choice is TWO"
;;
"three")
echo "Your choice is THREE"
;;
*)
echo "Usage (one|two|three)"
;;
esac
循环语句:
第一种方式:while do done
结构:
while [ condition ]
do
程序段落
done
说明:当conditions满足执行;否则终止
例子:
#!/bin/bash
#Program
# Use loop to try find your input
#History:
#2010/12/24 Tom first release
PATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/bin
export PATH
while [ "$yn" != "yes" ]&&[ "$yn" != "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done
第二种方式:until do done
结构:
until [ condition ]
do
程序段落
done
说明:当conditions不满足执行;否则终止
例子:
#!/bin/bash
#Program
# Use loop to try find your input
#History:
#2010/12/24 Tom first release
PATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/bin
export PATH
until [ "$yn" == "yes" ]||[ "$yn" == "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done
第三种方式:for...do....done
结构:
for ((初始值;限制值;执行布长))
do
程序段
done
说明:适用于数值运算
#!/bin/bash
#Program
# Try do calculate 1+2+3....+100
#History:
#2010/12/24 Tom first release
PATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/bin
export PATH
s=0
for ((i=1;i<=100;i++))
do
s=$(($s+$i))
done
echo "The result of '1+2+3...+100' is ==> $s"
#!/bin/bash
for ((i=1;i<10000000;i++))
do
touch $i;
done
作用:在文件夹下产生大量空文件;
目的:演示inode的功能,如果inode被用完,即使有空间,也无法新建文件。
补充:查看inode:df -i
#!/bin/bash
for ((i=1; i < 100000; i++))
do
echo -n 'A' >> log.txt;
sleep 1;
done
作用:每秒在文件log.txt中追加一个'A'
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29578568/viewspace-2136953/ ,如需转载,请注明出处,否则将追究法律责任。