频道栏目
首页 > 资讯 > Sybase > 正文

shell基础

15-06-23        来源:[db:作者]  
收藏   我要投稿

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:/bin
export PATH
echo -e 'HELLO WORLD ! \a\n'
exit 0

查看一个指令执行成功与否,可以使用?这个变量来观察echo? 输出0


#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

read -p 'please input you first name:' firstname
read -p 'please input you last name:' lastname
echo "\n you name is : $firstname $lastname"

exit 0



fuhui@ubuntu:~/script$ find -name 'file*' | xargs rm 

#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
echo  "i will use touch command to create 3 file \a\n"
read -p "please input your filename:" filename
filename=${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 /ls
test -e /lss && echo 'exist' || echo 'no exist'


test -z string 判读字符串是否为空,若为空,返回true
test -n string 判读字符串是否为空,若为空,返回true

#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

read -p 'please input a filename: ' file

test -z $file && echo "input can't be empty" && exit 0
test ! -e $file &&  echo "the $file  do not  exist" && exit 0

test -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.sh
the filename: sh02.sh is a regular file
AND the permissions is a  writable
fuhui@ubuntu:~/script$ [ -z "$HOME" ]; echo $? 
[] 内部两边必须有空格

fuhui@ubuntu:~/script$ echo $HOME
/home/fuhui

下面的有问题

#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
read -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 0
echo 'i do not know what your coice is ' && exit 0

please input(Y/N:)y
sh06.sh: 6: [: y: unexpected operator
sh06.sh: 7: [: y: unexpected operator
i do not know what your coice is 


#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

read -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 0

echo '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 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

echo "the script name : $0"
echo "the number of parameters is : $#"
test $# -lt 2 && echo 'parameters is too less' && exit 0
echo "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 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

echo "the script name : $0"
echo "the number of parameters is : $#"
$(test $# -lt 2) && echo 'parameters is too less' && exit 0
echo "the paramters are: $@"
shift
echo "the first parameter is: $1"
shift
echo "the second parameter is: $2"

fuhui@ubuntu:~/script$ sh sh08.sh -s atart
the script name : sh08.sh
the number of parameters is : 2
the paramters are: -s atart
the first parameter is: atart
the second parameter is: 

每次进行shift,命令的参数都会发生变化

#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

read -p "please input(Y/N:)" yn

if [ "$yn" = "Y" ] ||[ "$yn" = "y" ]; then
        echo 'ok,continue ' 
        exit 0
fi
if [ "$yn" = 'N' ] || [ "$yn" = 'n' ]; then
        echo 'oh,interrupt!' 
        exit 0
fi
echo 'i do not know what your coice is ' && exit 0
**终于出来效果了,最主要的原因原来是不支持 == 号,
#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

read -p "please input(Y/N:)" yn

if [ "$yn" = "Y" ] ||[ "$yn" = "y" ]; then
        echo 'ok,continue ' 
        exit 0
elif [ "$yn" = 'N' ] || [ "$yn" = 'n' ]; then
        echo 'oh,interrupt!' 
        exit 0
else
        echo 'i do not know what your coice is ' && exit 0
fi
if-elif-else-if的例子
相关TAG标签
上一篇:【SQL】——触发器
下一篇:awk应用-获取文件的信息(一)
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站