SHELL笔记( 一)

1.获取终端信息
   1.1
       tput  cols  获取终端的列数
       tput  lines 获取终端的行数
       tput  longname 打印当前终端名
       tput cup 100 100 将光标移动到方位(100,100)处
       tput  setb no  设置终端背景色(其中,no可以在0到7之间取值) 
       tput bold  设置文本样式为粗体
       tput ed   删除当前光标位置到行尾的所有内容

2.时间
   date     Tue May 12 22:31:25 CST 2015
   date +%s   1431441173     (POSIX时间,UTC 1970年1月1日0点是纪元时间;总秒数)

   date --date " May 12  CST 2015" +%A   (结果为:Tuesday)
  

eg:计算执行命令所消耗的时间(注意空格)
#!/bin/bash
start=$(date +%s)
find / -name xiebo
end=$(date +%s)
echo the start value is $start
echo the end value is $end
difference=$((end-start))
echo time taken to execute commands is $difference seconds.

eg:使用tput和sleep从0开始计数到40
#!/bin/bash
echo -n Count:
tput sc   #记住当前光标的位置

count=0;
while true;
do
if [ $count -lt 40 ];
then let count++;
sleep 1;
tput rc  #返回记录的光标的位置
tput ed  # 删除当前光标位置到行尾的所有内容
echo -n $count;
else exit 0;
fi
done

 
3调试脚本   (使用选项-x,启动跟踪调试shell脚本)
     3.1 全部跟踪调试
                   bash -x script.sh
     3.2 部分跟踪调试
               set -x :在执行时显示参数和命令。
               set +x :禁止调试
               set -v :当命令进行读取时显示输入
               set +v :禁止打印输入
           
             eg:
 
#!/bin/bash
for i in {1..6}
do
set -x
echo $i
set +x
done
echo "Script executed"

    3.3通过调试参数来进行调试      
#!/bin/bash
#
script.sh    
function DEBUG()
{
  [ "$_DEBUG" == "on" ] && $@ || :
}
for i in {1..10}
do
DEBUG echo $i
done

执行命令调试: _DEBUG=on ./script.sh 


4.循环
4.1 for循环
for var in list;  # list can be a string,or a sequence
do
commands;  #使用变量$var
done 

4.2 while循环
while condition
do
commands;
done

4.3 until 循环
x=0;
until [ $x -eq 9 ];
do let x++;
echo $x;
done

5.比较与测试
5.1  if条件
if condition;
then
commands;
fi
---------------
if condition;
then
commands;
elif  conditon;
then
commands;
else
commands
fi

5.2逻辑运算
[ condition ] && action;   # 如果condition为真,则执行action
[ condition ] || action;     #如果condition为假,则执行action



6录制并回放终端会话
6.1录制终端
  $ script -t 2> timing.log -a output.session
    commands;
    ......
   .......
   exit

//文件timing.log用于保存时序信息;文件output.session用于存储命令输出。

6.2回放终端
   $ scriptreplay timing.log output.session
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值