shell 和shell script
- 从程序员角度看,shell本身是一种用C语言编写的程序,从用户角度看,shell是用户与Linux操作系统沟通的桥梁。用户既可以输入命令执行,又可以利用shell脚本编程,完成更加复杂的操作
- shell script 是一种弱语言类型,使用变量时无需首先声明其类型,也无需事先定义,shell在对变量取值时要在变量名前加$
wangaifei@ubuntu18:~/lingyun$ bash hello.sh
hello world
wangaifei@ubuntu18:~/lingyun$ ./hello.sh
-bash: ./hello.sh: Permission denied
wangaifei@ubuntu18:~/lingyun$ chmod a+x hello.sh
wangaifei@ubuntu18:~/lingyun$ ./hello.sh
hello world
wangaifei@ubuntu18:~/lingyun$
#!/bin/bash
str="hello world"
echo $str
var1=5
var2=6
total=`expr $var1 + $var2`
echo $total
wangaifei@ubuntu18:~/lingyun$ ./hello.sh
hello world
11
shell判断
- gt:>
- lt:<
- eq:=
- ge:>=
- le:<=
if [ $var -get 5 ];then \\
echo ···\\
elif [ $var -eq 5 ];then\\
···\\
elif···
fi
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lXBlmrY0-1646456193468)(https://note.youdao.com/yws/res/8/WEBRESOURCE84487313b115500d2408583c3d987138)]
shell循环
!/bin/bash
for var in `seq 1 10`;do
echo "hello $var"
done
wangaifei@ubuntu18:~/lingyun/shell$ ./xunhuan.sh
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
hello 8
hello 9
hello 10
!/bin/bash
for file in `ls *.txt`
do
echo "file:$file"
mv $file `echo $file|cut -d. -f1`.c
done
wangaifei@ubuntu18:~/lingyun/shell/test$ ./bash.sh
file:haha.txt
file:hehe.txt
file:hello.txt
wangaifei@ubuntu18:~/lingyun/shell/test$ ls
bash.sh haha.c haha.mp4 hehe.c hello.c
#!/bin/bash
sum=0;
for i in `seq 1 100`
do
sum=`expr $i + $sum`
i=`expr $i + 1`
done
echo $sum
wangaifei@ubuntu18:~/lingyun/shell/test$ ./plus.sh
5050
shell 参数解析
#!/bin/bash
echo "arguments count:$#"
echo "arguments list:$* "
if [ $# -ne 2 ];then
echo "usage : $0 [start] [end]"
exit;
fi
statr=$1
end=$2
if [ $start -ge $end ];then
echo "error: $1 >= $2"
exit;
fi
sum=0;
for i in `seq $start $end`
do
sum=`expr $i + $sum`
i=`expr $i + 1`
done
echo $sum
wangaifei@ubuntu18:~/lingyun/shell/test$ ./canshu.sh 1 100
arguments count:2
arguments list:1 100
./canshu.sh: line 13: [: -ge: unary operator expected
5050
#!/bin/bash
function text()
{
echo "function arguments count: $#"
echo "function arguments list:$*"
echo "list argument:$1"
echo "list argument:$2"
}
text haha hh
echo "arguments count:$#"
echo "arguments list:$* "
if [ $# -ne 2 ];then
echo "usage : $0 [start] [end]"
exit;
fi
statr=$1
end=$2
if [ $start -ge $end ];then
echo "error: $1 >= $2"
exit;
fi
sum=0;
for i in `seq $start $end`
do
sum=`expr $i + $sum`
i=`expr $i + 1`
done
echo $sum
wangaifei@ubuntu18:~/lingyun/shell/test$ ./canshu2.sh 1 100
function arguments count: 2
function arguments list:haha hh
list argument:haha
list argument:hh
arguments count:2
arguments list:1 100
./canshu2.sh: line 26: [: -ge: unary operator expected
5050
shell其他应用
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vs5AzmRT-1646456193470)(https://note.youdao.com/yws/res/6/WEBRESOURCEe5e4e19385357992af1a525b4f0d1456)]
set -x
text haha hh
set +x
source 引入文件内容的变量和值
source ifcfg-eth0
echo "$ONBOOT"
echo "ifconfig eth0 $IPADDR"
linux常见服务器搭建
ftp服务
https://note.youdao.com/s/QMI96q9u
LNMP
https://note.youdao.com/s/W8RbZLOs