什么是shell
shell是一类编程语言,它是一种软件实现,由软件系统提供给用户的命令行界面,实现人机交互。
在linux下常用的脚本语言有bash sh
shell的脚本代码不用编译链接就可以运行了。
简单的例子:
#!/bin/sh
#comment
echo "hello world"
注意:/bin/sh是一个路径,也可以是/bin/bash,就看你用什么解析器去解析shell脚本,用sh就/sh,用bash就/bash
在linux命令行下运行shell
source xx.sh
或者
bash xx.sh
变量定义
赋值
用等号进行赋值,注意等号两端不能有空格。
引用或者说是解析
用$符号进行引用,注意,在 $ 后面跟上一个字符串,字符串会看做是变量被解析,如果这个字符串没有被定义,则认为这是一个定义为空的变量。
另外
${val} 和 $val是有区别的, $val能用的话 ${val}也能用。
str="helloworld"
echo $str
转义字符
如果要输出转义字符的话,就要在字符串两端加上双引号或者单引号。
执行linux下的命令
1.直接执行
2.用反引号(键盘Esc下面,数字键1坐标的那个波浪线和点,那个点就是反引号)
注意,懒得在.sh里面截图又再截图一个输出,所以直接在linux命令行下执行。
第一种方式是直接执行,
第二种方式是用反引号执行。
反引号执行例子
#!/bin/sh
# comment usage of ` `is the key which under Esc or beside key 1/!
# example : command pwd of linux
# catuion! the different between ` and '
PWD=`pwd`
echo $PWD
MYPATH="`pwd`/include"
echo "MYPATH = $MYPATH"
#if use ' instead of " then ''did not work
MYPATH2='`pwd`/include'
echo "MYPATH2 = $MYPATH2"
运行结果:
if的用法
蹩脚英语,因为我ubuntu没装输入法,只能这样写笔记,将就着看。
#!/bin/sh
2 # comment
3
4 #**************************************************#
5 # comment usage of if (the shortening of if with using && or ||)
6 # -o means logic or operation && ||
7 # caution 1. inside [] you should put a ' ' after '[' and before ']'
8 # caution 2. str must exist
9
10 str="123"
11 [ -z $str ] || echo "str is not empty."
12
13 str2=""
14 [ -z $str2 ] && str2="abcd"
15 echo $str2
16
17 #**************************************************#
18 # comment usage of if -o
19 # -o means logic or operation --- o or-----
20 # caution 1. inside [] you should put a ' ' after '[' and before ']'
21 # caution 2. str must exist
22 str=""
23 if [ 12 -eq 12 -o "abdc"="abc" ];then
24 echo "yes"
25 else
26 echo "no"
27 fi
28
29 #**************************************************#
30 # comment usage of if -z
31 # see if str is empty or not
32 # caution 1. inside [] you should put a ' ' after '[' and before ']'
33 # caution 2. str must exist
34 str=""
35 if [ -z $str ];then
36 echo "yes"
37 else
38 echo "no"
39 fi
40
41 #**************************************************#
42 # comment usage of if -eq -gt -lt -ge -le
43 # see if num_a and num_b is equal(-eq) greater(-gt) lessthen(-lt)
44 # greater equal(-ge) lessthen equal (-le)
45 # caution inside [] you should put a ' ' after '[' and before ']'
46 if [ 3 -lt 4 ];then
47 echo "yes"
48 else
49 echo "no"
50 fi
51
52 #**************************************************#
53 # comment usage of if =
54 # see if string_a is equal to string_b
55 # caution inside [] you should put a ' ' after '[' and before ']'
56 if [ "strabc" = "strdef" ];then
57 echo "equal"
58 else
59 echo "not equal"
60 fi
61
62 #**************************************************#
63 # comment usage of if -d
64 # see if dir is exist or not
65 # caution inside [] you should put a ' ' after '[' and before ']'
66 if [ -d vmsd ];then
67 echo "yes,current dir has a dir called vmsd"
68 else
69 echo "no,current dir hasn't got a dir called vmsd"
70
71 fi
72
73 #*************************************************#
74 # comment usage of if -f
75 # see if file a.txt is exist or not
76 # caution inside [] you should put a ' ' after '[' and before ']'
77 if [ -f a.txt ];then
78 echo "yes,file a.txt exist in current dir"
79 else
80 echo "no,file a.txt not exist in current dir"
81 touch a.txt
82 fi
循环的用法
#!/bin/sh
#loop for while 输出 1 2 3 4 5 ,改变i初始值或者改变$i+n,n=1、n=2的值就输出奇数偶数。
i=1
j=11
while [ $i -lt $j ];do
echo $i
i=$(($i+1))
done
#loop for 输出所有ls到的目录,
for i in `ls`
do
echo $i
done
#loop for 输出1 2 3 4 5
for i in 1 2 3 4 5
do
echo $i
done
echo的用法补充
echo可以新建一个文件并且往文件里面输入、添加内容
#!/bin/sh
# use echo to make a file and fill some data into it
# create a file called a.txt and put "test" into a.txt
# Example
echo "test" > a.txt
#add data at the end of the file
echo "add data" >> a.txt
switch case用法
#!/bin/sh
#comment
#case
var=2;
case $var in
1) echo "1" ;;
2) echo "2" ;;
esac
传参
#!/bin/sh
#comment lalalalala
echo $# $0 $1 $2 $3
结果
符号 | 结果 |
---|---|
$# | 传参个数 |
$0 | shell文件名 |
$1 | 第一个参数 |
$2 | 第二个参数 |
$3 | 第三个参数 |
与C语言比较,如果是C语言的话会认为传参个数是4个,把执行的文件也算一个参数,这里是shell与C的一点区别。
shell中的break和shift
shell中的break用于跳出循环,而不是在case中跳出case
shell中的shift是移位的意思,往左移动一位。
这两个总结得不怎么详细,具体还是最好看别人的博客做更详细的学习。