Shellshellshell命令学习笔记

linux学习之shell

2017-08-08  本文已影响41人  Java面试官

简介:
在这里所说的shell是一种为 shell 编写的脚本程序。业界所说的 shell 通常都是指 shell 脚本,但读者朋友要知道,shell 和 shell script 是两个不同的概念。shell指的是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。本文出现的 "shell编程" 都是指 shell 脚本编程,不是指开发 shell 自身。

编程环境:
ubuntu14.04,本人最近爱上的一个新的系统,已经废了windows,不再爱了!

先给个demo:

rice@rice:~$ echo "Hello world"
Hello world

尝试下将代码保存在文本,并命名为 test.sh,运行:

rice@rice:/media/rice/documents/demo$ ./test.sh
bash: ./test.sh: Permission denied

解决方法:

rice@rice:/media/rice/documents/demo$ chmod +x ./test.sh
rice@rice:/media/rice/documents/demo$ ./test.sh
Hello World !

采用chmod +x ./test.sh 使脚本具有执行权限
那么为什么运行的时候要写成 ./test.sh 而不是 test.sh,是因为如果直接写成test.sh,linux系统会去PATH里寻找有没有叫test.sh的,而不是当前目录查找。

基础类型和操作:

Shell变量

变量命名要求:

如: my_name="rice" #变量名和等号之间不可以有空格

使用变量:

my_name="rice"
echo $my_name
echo ${my_name}

运行:

rice@rice:/media/rice/documents/demo$ ./test.sh
rice
rice

说说变量的多个用法:
只读变量:readonly 变量名 #变量不可被修改
删除变量:unset 变量名 #变量被清空

说说变量的类型:

字符串
shell的字符串和java等的一样,这里不多累赘,只说说用法:

拼接字符串:
给个demo:

your_name="rice"
greeting="hello, "$your_name" !"
greeting_1="hello, ${your_name} !"
echo $greeting $greeting_1

运行后输出:

rice@rice:/media/rice/documents/demo$ ./test.sh
hello, rice ! hello, rice !

获取字符串长度:${#字符串名}

shell数组:数组名=(值1 值2 ... 值n)
读取:value=${array_name[n]}

Shell 注释:#xxx

Shell 传递参数
我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n。n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推……

给出demo:

echo "Shell 传递参数实例!";
echo "第一个参数为:$1";
echo "第二个参数为:$2";
echo "第三个参数为:$3";

运行,结果输出:

rice@rice:/media/rice/documents/demo$ ./test.sh 1 2 3
Shell 传递参数实例!
第一个参数为:1
第二个参数为:2
第三个参数为:3

说说shell常用命令:
echo : 打印输出,如 echo "Hello Shell"
printf:printf 命令模仿 C 程序库(library)里的 printf() 程序。如:

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 

运行,输出:

rice@rice:/media/rice/documents/demo$ ./test.sh 1 2 3
姓名     性别   体重kg
郭靖     男      66.12
杨过     男      48.65
郭芙     女      47.99

其中:
%s %c %d %f都是格式替代符
%-10s 指一个宽度为10个字符(-表示左对齐,没有则表示右对齐),任何字符都会被显示在10个字符宽的字符内,如果不足则自动以空格填充,超过也会将内容全部显示出来。
%-4.2f 指格式化为小数,其中.2指保留2位小数。

shell流程控制

if 语句语法格式:(末尾的fi就是if倒过来拼写,表示if结束)

if 条件
then
    操作1 
    操作2
    ...
fi

if else 的时候:

if 条件
then
    操作1 
    操作2
else
    操作3
fi

if else-if else的时候:

if 条件1
then
    操作1
elif 条件2 
then 
    操作2
else
    操作n
fi

for循环操作:
格式如下:

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
don

while 语句:
格式如下:

while 条件
do
    操作
done

shell函数:
shell 可以用户定义函数,然后在shell脚本中可以随便调用。
如:

testFunc(){
    echo "hello shell func!"
}
echo "-----函数开始执行-----"
testFunc
echo "-----函数执行完毕-----"

运行,输出如下结果:

rice@rice:/media/rice/documents/demo$ ./test.sh
-----函数开始执行-----
hello shell func!
-----函数执行完毕-----

再给出个demo,带有return语句的func,并且读取命令行输入:

funWithReturn(){
    echo "这个函数会对输入的两个数字进行相加运算..."
    echo "输入第一个数字: "
    read aNum #读取
    echo "输入第二个数字: "
    read anotherNum 
    echo "两个数字分别为 $aNum 和 $anotherNum !"
    return $(($aNum+$anotherNum))
}
funWithReturn
echo "输入的两个数字之和为 $? !"

运行,输出结果:

rice@rice:/media/rice/documents/demo$ ./test.sh
这个函数会对输入的两个数字进行相加运算...
输入第一个数字: 
1
输入第二个数字: 
2
两个数字分别为 1 和 2 !
输入的两个数字之和为 3 !

其中 函数返回值在调用该函数后通过 $? 来获得。

那么如何传参呢,func的传参和shell的传参很像,也是$n来表示,demo如下:

funWithParam(){
    echo "第一个参数为 $1 !"
    echo "第二个参数为 $2 !"
    echo "第十个参数为 $10 !"
    echo "第十个参数为 ${10} !"
    echo "第十一个参数为 ${11} !"
    echo "参数总数有 $# 个!"
    echo "作为一个字符串输出所有参数 $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73

运行,结果输出:

rice@rice:/media/rice/documents/demo$ ./test.sh
第一个参数为 1 !
第二个参数为 2 !
第十个参数为 10 !
第十个参数为 34 !
第十一个参数为 73 !
参数总数有 11 个!
作为一个字符串输出所有参数 1 2 3 4 5 6 7 8 9 34 73 !

重头戏,shell输入\输出重定向

command > file  将输出重定向到 file。
command < file  将输入重定向到 file。
command >> file     将输出以追加的方式重定向到 file。
n > file    将文件描述符为 n 的文件重定向到 file。
n >> file   将文件描述符为 n 的文件以追加的方式重定向到 file。
n >& m  将输出文件 m 和 n 合并。
n <& m  将输入文件 m 和 n 合并。
<< tag  将开始标记 tag 和结束标记 tag 之间的内容作为输入。

以第一个为例子,给出demo:
目前我的test.sh文件内容是:

rice@rice:/media/rice/documents/demo$ cat test.sh
funWithParam(){
    echo "第一个参数为 $1 !"
    echo "第二个参数为 $2 !"
    echo "第十个参数为 $10 !"
    echo "第十个参数为 ${10} !"
    echo "第十一个参数为 ${11} !"
    echo "参数总数有 $# 个!"
    echo "作为一个字符串输出所有参数 $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73

运行如下代码后后:

rice@rice:/media/rice/documents/demo$ echo "我改啦" > test.sh

内容变为:

rice@rice:/media/rice/documents/demo$ cat test.sh
我改啦
上一篇下一篇

猜你喜欢

热点阅读