Linux-shell基础-1

来源:互联网 发布:360网页广告拦截软件 编辑:程序博客网 时间:2024/05/16 01:12

shell script 一次处理多个系统指令,不需要编译即可以执行


自动化管理的重要依据
追踪和管理系统的重要工作
简单入侵的检测工具
联系指令单一化
简易的数据处理
跨平台支持


shell script在系统管理上是一项好工具,但是在处理大量数据运算上,就不够好。


指令的执行从上倒下,从左到右
指令 选项和参数之间的多个空白都会被忽略掉
空白行将被忽略掉 且tab视为一个空格
如果读取到一个Enter(CR),就尝试开始执行该行命令
如果一行指令太多,使用\Enter 延伸到下一行执行 #可以作为批注


执行
绝对路径/相对路径/以bash程序来执行 sh / bash
利用sh的参数 -n及-x来检查shell语法是否正确


第一行#!/bin/bash
因为我们使用的是bash,所以必须以#!/bin/bash来宣告档案的语法使用bash语法
注释行#除了第一行之外,其他的#都是批注的功能
主要环境变量的宣告:
将一些重要的环境变量设定好PATH和LANG是当中最重要的。因为可以让程序直接下达外部指令,而不必写绝对路径


我的export
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games”


#!/bin/bash# program:test example 01# 空格是有实际意义的,需要将 = 两边的空格移除PATH = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binexport PATHecho -e 'HELLO WORLD ! \a\n'exit 0

查看一个指令执行成功与否,可以使用?echo? 输出0


#!/bin/bash# program:test example 01PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binexport PATHread -p 'please input you first name:' firstnameread -p 'please input you last name:' lastnameecho "\n you name is : $firstname $lastname"exit 0fuhui@ubuntu:~/script$ find -name 'file*' | xargs rm 

#!/bin/bash# program:test example 01PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binexport PATHecho  "i will use touch command to create 3 file \a\n"read -p "please input your filename:" filenamefilename=${filename:-"filelog"}date1=$(date --date='2 days ago' +%Y%m%d)date2=$(date --date='1 days ago' +%Y%m%d)date3=$(date +%Y%m%d)file1=${filename}${date1}  #file2=${filename}${date2}file3=${filename}${date3}touch "$file1"touch "$file2"touch "$file3"exit 0

$((计算式))

A=B
echo ${A}B 输出BB

直接使用echo $$((3*2)) 进行计算式


直接使用echo $$((3*2)) 进行计算式

source 和 bash 执行的不同效果:
1. bash是在一个子进程中执行的,设定的变量在执行之后会释放
2. source 执行在环境中设定的变量依然存在(但是我这里系统连接断掉了,类似于ctrl+d的效果)

我们提刡过 $? 这个所代表的意思, 此外,也透过 && 及 || 来作为前一个指令执行回传值对亍后一个指令是否要迚行的依据。


test用户测试系统某些档案或者属性test -e /lstest -e /lss && echo 'exist' || echo 'no exist'test -z string 判读字符串是否为空,若为空,返回truetest -n string 判读字符串是否为空,若为空,返回true

#!/bin/bash# program:test example 01PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binexport PATHread -p 'please input a filename: ' filetest -z $file && echo "input can't be empty" && exit 0test ! -e $file &&  echo "the $file  do not  exist" && exit 0test -f $file && filetype='regular file'test -d $file && filetype='directory'test -w $file && perm="$perm writable"test -x $file && perm="$perm executable"echo "the filename: $file is a $filetype"echo "AND the permissions \is a $perm"

查找的文件都在执行文件的内部,外部的文件还是访问不了,不知道为什么


please input a filename: sh02.shthe filename: sh02.sh is a regular fileAND the permissions is a  writable
fuhui@ubuntu:~/script$ [ -z "$HOME" ]; echo $? [] 内部两边必须有空格fuhui@ubuntu:~/script$ echo $HOME/home/fuhui

下面的有问题

#!/bin/bash# program:test example 01PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binexport PATHread -p "please input(Y/N:)" yn[ $yn == "Y" -o $yn == 'y' ] && echo 'ok,continue ' && exit 0[ $yn == 'N' -o $yn == 'n' ] && echo 'oh,interrupt!' && exit 0echo 'i do not know what your coice is ' && exit 0please input(Y/N:)ysh06.sh: 6: [: y: unexpected operatorsh06.sh: 7: [: y: unexpected operatori do not know what your coice is #!/bin/bash# program:test example 01PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binexport PATHread -p "please input(Y/N:)" yn[ "$yn"=="Y" -o "$yn"=="y" ] && echo 'ok,continue ' && exit 0[ "$yn"=='N' -o "$yn"=='n' ] && echo 'oh,interrupt!' && exit 0echo 'i do not know what your coice is ' && exit 0没错误了,但是又不能运行了............

fuhui@ubuntu:~/script$ file /etc/init.d/etc/init.d: directory fuhui@ubuntu:~/script$ file /etc/init.d/syslog/etc/init.d/syslog: ERROR: cannot open `/etc/init.d/syslog' (No such file or directory)fuhui@ubuntu:~/script$ 
默认参数scriptname opt1 opt2 opt3$01$1  $2   $3
#!/bin/bash# program:test example 01PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binexport PATHecho "the script name : $0"echo "the number of parameters is : $#"test $# -lt 2 && echo 'parameters is too less' && exit 0echo "the paramters are: $@"echo "the first parameter is: $1"echo "the second parameter is: $2"

直接使用test #@ -lt 2 进行比较,为什么没有使用$(test #@ -lt 2 ) ,试了一下,都可以

shift可以移动变量,而且shift后面可以接数字,代表拿掉前面几个参数的意思


#!/bin/bash# program:test example 01PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binexport PATHecho "the script name : $0"echo "the number of parameters is : $#"$(test $# -lt 2) && echo 'parameters is too less' && exit 0echo "the paramters are: $@"shiftecho "the first parameter is: $1"shiftecho "the second parameter is: $2"fuhui@ubuntu:~/script$ sh sh08.sh -s atartthe script name : sh08.shthe number of parameters is : 2the paramters are: -s atartthe first parameter is: atartthe second parameter is: 每次进行shift,命令的参数都会发生变化

#!/bin/bash# program:test example 01PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binexport PATHread -p "please input(Y/N:)" ynif [ "$yn" = "Y" ] ||[ "$yn" = "y" ]; then        echo 'ok,continue '         exit 0fiif [ "$yn" = 'N' ] || [ "$yn" = 'n' ]; then        echo 'oh,interrupt!'         exit 0fiecho 'i do not know what your coice is ' && exit 0
**终于出来效果了,最主要的原因原来是不支持 == 号,
#!/bin/bash# program:test example 01PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binexport PATHread -p "please input(Y/N:)" ynif [ "$yn" = "Y" ] ||[ "$yn" = "y" ]; then        echo 'ok,continue '         exit 0elif [ "$yn" = 'N' ] || [ "$yn" = 'n' ]; then        echo 'oh,interrupt!'         exit 0else        echo 'i do not know what your coice is ' && exit 0fiif-elif-else-if的例子
0 1
原创粉丝点击