初涉linux(三)之shell编程

[color=red]如果系统学习,使用<<高级Bash脚本编程指南>>这本书[/color]
shell初级编程 by hayabusa
3.1讲义
3.1.1 shell
set //所有变量
unset //取消变量设置
export 变量 //导出变量,使其全局可用
env //环境变量
shell基本格式:
#!/bin/bash
//edit...
chmod u+x script

$(command)或者`command` //命令嵌套的写法
3.1.2
3.1.2.1
stdin 标准输入
stdout 标准输出
stderr 标准错误输出
3.1.2.2重定向:
> 输出重定向
2> 错误重定向
&> 全部重定向
tr 'a-z' 'A-Z' < var3.sh
3.1.2.3
管道:前者输出作为后者输入
ls -l /etc | less
3.1.2.4
多目标重定向
command |tee t2.txt
3.1.3正则表达式
^ $ *
3.1.4 shell 带参数
$1 $2 $* $#
3.1.5 条件判断
= -eq
!= -ne
-gt
-lt
-ge
-le
3.1.6控制流程 -a -o !

while 条件
do
命令
done

if 条件
then 命令(可以嵌套if)
elif 条件
then 命令
else
命令(可以嵌套if)
fi

@#!!//数值运算加两对圆括号
case
case 值 in
模式)
命令
;;
模式)
命令
;;
...
esac


for 变量 in 'seq 1 100'
do
command
done
//break continue 类似于C:exit直接退出,可带返回值

until 条件
do
//command
done
3.2 扩展
3.2.1
读老师的程序偷师到的几个命令 :wink:
basename /path/filename //printf the real name of a file
cut -d: -f2 // 这个命令用好了非常的强大 :wink:
3.2.2 userlist.sh
# list all the users
#!/bin/bash
n=0;
for i in `cut -d: -f3 /etc/passwd`
do
((n=n+1))
m=`echo "$n " |tr ' ' 'p'`
if [ $i -le 60000 -a $i -ge 500 ]
then cut -d: -f1 /etc/passwd|sed -n $m
elif [ $i = 0 ]
then cut -d: -f1 /etc/passwd|sed -n $m
fi
done
3.3 问题
3.3.1
caseselect 文件中的此句(鄙视自己,看了解释还不懂 :cry: )
echo "`basename $0`:This is not between 1 and 5" >&2
[color=red] 已解决:
这句话的意思就是将stdout重定向到stderr之中,但是由于stdout与stderr的默认输出都是monitor,所以看不到效果,下边的代码好些:
echo "`basename $0`:This is not between 1 and 5" 2>ver >&2[/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值