Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2266644
  • 博文数量: 168
  • 博客积分: 6641
  • 博客等级: 准将
  • 技术积分: 1996
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-02 11:49
文章存档

2020年(4)

2019年(6)

2017年(1)

2016年(3)

2015年(3)

2014年(8)

2013年(2)

2012年(12)

2011年(19)

2010年(10)

2009年(3)

2008年(17)

2007年(80)

分类: LINUX

2007-11-23 17:51:18

几则shell编程小技巧备忘

 2007-11-24整理 (部分来自Internet)  TsengYia#126.com 一路狂笑

###########################################################################################

1、按任意键继续的shell代码1 【等待10秒钟或按任意键继续,不用再按Enter键确认】
#!/bin/bash
read -p "Wait 10 seconds or Press any key to continue..." -sn 1 -t 10 CHAR
echo
echo "Now Continue to ..."
#..... some other scripts ......


2、按任意键继续的shell代码2【按任意键继续,不用再按Enter键确认】

#!/bin/bash
function char {
stty raw
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -raw
}
echo "Press any key to continue..."
input=$(char)

echo "Now Continue to ..."
#..... some other scripts ......

3、将输入的密码显示为*星号 【若字符输入速度过快,可能有个别字符来不及读入】
#!/bin/bash
getchar() {
        stty cbreak -echo
        dd if=/dev/tty bs=1 count=1 2> /dev/null
        stty -cbreak echo
}
printf "Please input your passwd: "
while true
do
        ret=`getchar`
        if [ -z $ret ]; then
                echo ; break
        fi
        str="$str$ret"
        printf "*"
done
echo
echo "Your password is: $str"

4、带小数的数值运算

echo "1.2345+5.4321" | bc
echo "1.23*3.1415926" | bc

5、文件倒序显示
假设有文件a.file,内容如下:
abc1
def2
hij3

则:
正常顺序:cat a.file
abc1
def2
hij3

全文行倒序:tac a.file        //先显示最后一行,然后倒数第二行……,第一行
hij3
def2
abc1

行内字符倒序:rev a.file
1cba
2fed
3jih

阅读(1566) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~