shell 脚本学习笔记

前言

基于bash 并参考 菜鸟教程的shell 教程 http://www.runoob.com/linux/linux-shell.html

### shell 的执行方法
chmod +x ./test.sh  
bash test.sh

输入输出

调试输出
#!/bin/bash
set -x
外部参数
#!/bin/bash
# author:菜鸟教程
# url:www.runoob.com
echo "Shell 传递参数实例!";  ## echo 为输出方法
echo "执行的文件名:$0";
echo "第一个参数为:$1";
echo "第二个参数为:$2";
echo "第三个参数为:$3";

$ chmod +x test.sh 
$ ./test.sh 1 2 3
Shell 传递参数实例!
执行的文件名:./test.sh
第一个参数为:1
第二个参数为:2
第三个参数为:3
printf 格式化输出
#!/bin/bash
# author:菜鸟教程
# url:www.runoob.com

printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg  
printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234 
printf "%-10s %-8s %-4.2f\n" 杨过 男 48.6543 
printf "%-10s %-8s %-4.2f\n" 郭芙 女 47.9876 

详细见 http://www.runoob.com/linux/linux-shell-printf.html

重定向
标准输入及标准输出,默认均为当前用户操作的终端。

command > file
command < file
command >> file

需要注意的是文件描述符 0 通常是标准输入(STDIN),1 是标准输出(STDOUT),2 是标准错误输出(STDERR)。

command 2 > file   ##重定向错误到file
command > file 2>&1     ## 标准输出及错误都重定向到file
command > /dev/null 2>&1  ## 不显示任何输出
外部文件
. filename   # 注意点号(.)和文件名中间有一空格
或
source filename

变量及赋值

参见 http://www.runoob.com/linux/linux-shell-variable.html

运算

算数、关系(大小)、布尔(与或非)、逻辑(与或)、字符串、文件测试
http://www.runoob.com/linux/linux-shell-basic-operators.html 
正则
https://www.cnblogs.com/hanxiaoyu/p/5759477.html

数组

参见:http://www.runoob.com/linux/linux-shell-array.html

流程控制

参见:http://www.runoob.com/linux/linux-shell-process-control.html

函数

参见:http://www.runoob.com/linux/linux-shell-func.html

常用系统命令

http://www.runoob.com/linux/linux-command-manual.html
sed、tail、grep、wc、awk、more、cat

应用进阶

用户交互 read
http://www.runoob.com/linux/linux-comm-read.html
图形交互 dialog
http://www.ttlsa.com/linux-command/linux-dialog-shell/
自动交互 expect
http://www.cnblogs.com/lixigang/articles/4849527.html

猜你喜欢

转载自blog.51cto.com/13673090/2331402
今日推荐