码迷,mamicode.com
首页 > 系统相关 > 详细

shell脚本编程

时间:2017-04-11 13:11:08      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:ash   变量   开头   bin   val   数组元素   格式   时间   介绍   

连续两个!表示执行上一条命令
!n这里的n是数字,表示执行命令历史中第n条命令
!字符串 !pw表示执行命令历史中最近的以pw开头的命令
*匹配零个或多个字符 ,?匹配一个字符
输入重定向 > >> < 2> 2>>
ctrl+z 使任务暂停 fg命令恢复 bg把任务丢到后台 jobs可以查看被暂停或者在后台运行的任务
 
方法一:直接运行命令export PATH=$PATH:/usr/local/webserver/php/bin 和 export PATH=$PATH:/usr/local/webserver/mysql/bin
使用这种方法,只会对当前会话有效,也就是说每当登出或注销系统以后,PATH 设置就会失效,只是临时生效。
 
方法二:执行vi ~/.bash_profile修改文件中PATH一行,将/usr/local/webserver/php/bin 和 /usr/local/webserver/mysql/bin 加入到PATH=$PATH:$HOME/bin一行之后
这种方法只对当前登录用户生效
 
方法三:修改/etc/profile文件使其永久性生效,并对所有系统用户生效,在文件末尾加上如下两行代码
PATH=$PATH:/usr/local/webserver/php/bin:/usr/local/webserver/mysql/bin
export PATH
 
最后:执行 命令source /etc/profile或 执行点命令 ./profile使其修改生效,执行完可通过echo $PATH命令查看是否添加成功
 
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2015-01/111727.htm
 ----------------------------------------------------------------------------------------------------------
shell介绍
A shell脚本只能在Linux平台上运行
B shell只是脚本语言,依赖于Linux shell环境
C shell执行效率比较低下,远远没有C快
D shell可以帮我们实现自动化运维
----------------------------------------------------------------
shell结构以及执行
vi 1.sh
#!/bin/bash
##描述
 
运行:
bash 1.sh sh 1.sh
相对路径:chmod a+x 1.sh ./1.sh
绝对路径:/root/shell/1.sh
差看脚本执行过程 sh -x 1.sh
-----------------------------------------------------------
date命令
直接输出日期和时间
cal查看日历 -y查看一整年
date -s "年-月-日 时:分:秒"
 
yum install -y ntp 同步时间
ntpdate time.windows.com 微软时间服务器
ntpdate ntp.fudan.edu.cn 复旦时间服务器
 
+%F 年月日
+%T 时间
+%Y 四位年
+%y 两位年
+%m 月份
+%d 日期
+%H 小时
+%M 分钟
+%S 秒
+%s 距离1970年1月1日0时0秒0分一共过了多少秒
date +"+%Y-%m-%d %H:%M:%S"
 -----------------------------------------------------------------
数学运算
#!/bin/bash
a=1
b=2
sum=$[$a+$b]
echo "$a+$b=$sum"
 
#!/bin/bash
a=1;b=2
c=$[$a*$b]; echo $c
 
和用户交互
#!/bin/bash
##Using `read` in shell script
read -p "please input a number:" x
read -p "please input another number:" y
sum=$s[$x+$y]
echo "The sum of the two numbers is: $sum"
read命令为和用户交互
------------------------------------------------------
if判断的几种用法
1.和文档相关的判断
-e 判断文件或目录是否存在
-d 是不是目录,并是否存在
-f 是否为普通文件,并存在
-r 文档是否有度权限
-w 是否有写权限
-x 是否可执行
if [-e filename]; then
 
2.变量是否为空
-n选项可以判断一个变量是否为空,注意一定要把变量引起来,-z可以不用引起来
if [-n "$a"]; then echo "a is not null"; else echo "a is null"; fi
if [-z $a]; then echo "a is null"; fi
 
3.判断条件为一个命令
if grep -p ‘^aming:‘ /etc/passwd; then echo "user aming exist."; fi
grep -p 的作用是过滤但不输出
---------------------------------------------------------------------------
case选择
case 变量 in
value 1)
command
;;
value 2)
command
;;
value 3)
command
;;
*)
command
;;
Esac
不限制value的个数,*则代表除了上面的value外的其他值。
#!/bin/bash
read -p "Input a number:" n
a=$[$n%2]
case $a in
1)
echo "The number is odd"
;;
0)
echo "The number is even."
;;
*)
echo "It‘s not a number!"
;;
esac
----------------------------------------------------------------
循环
for循环
#!/bin/bash
for i in `seq 1 5`; do
echo $i
done
 
结构
for 变量名 in 循环条件; do
command
done
------------------------------------------------
while循环
常常用来写死循环的的脚本,用来监控某项服务
#!/bin/bash
a=5
while [$a -ge 1]; do
echo $a
a=$[$a-l]
done
 
while 条件; do
command
done
 
判断系统负载的死循环
#!/bin/bash
while:; do
load=`uptime |awk `{print $(NF -2)}`|cut -d. -f1`
if[ $load -gt 10 ]
then
echo "system load is hugh."|mail -s "system load" 111111@163.com
fi
sleep 10
done
-----------------------------------------------------------------------------------------------
shell中断继续退出
break直接结束本层循环
 
#!/bin/bash
for i in `seq 1 5`;do
echo $i
if [ $i == 3 ]
then
break
fi
echo $i
done
echo aaaaaa
 
continue忽略continue之下的代码,直接进行下一次循环
#!/bin/bash
for i in `seq 1 5`;do
echo $i
if [ $i == 3 ]
then
continue
fi
echo $i
done
echo $i
 
exit直接退出shell
#!/bin/bash
for i in `seq 1 5`;do
echo $i
if [ $i == 3 ]
then
exit
fi
echo $i
done
echo aaaaaaaa
---------------------------------------------------
shell函数
函数就是把一小段代码整理到一个小单元中,并给这个小单元起一个名字,当用到段代码的时候,直接调用这个小单元即可
格式
function f_() {
command
}
函数必须要放在最前面
vim func 1.sh
#!/bin/bash
input() {
echo $1
}
input aaa
 
加法运算函数
#!/bin/bash
sum() {
s=$[$1+$2]
echo $s
}
sum 1 2
 
#!/bin/bash
ip() {
ifconfig |grep -A1 "$1" |tail -1 |awk ‘{print $2}‘ |awk -F‘:‘ ‘{print $2}‘
}
read -p "Pleasr input the eth name:" e
myip=`ip $e`
echo "$e address is $myip"
----------------------------------------------------------------------
shell数组
数组定义,一对括号表示的是数组,数组元素用“空格”符号分割开
a=(1 2 3 4 5)
echo $a
1
 
获取数组的元素个数
echo ${#a[@]}
读取数组中的某一个元素,标数从0开始,a[0]是第一个元素
echo ${a[2]}
打印整个数组的元素,或者使用 echo ${a[@]}
echo ${a[*]}
 
数组赋值
a[1]=100
echo ${a[*]}
a[5]=100
echo ${a[*]}
 
数组的删除
a=(1 2 3 4 5)
unset a[1]
echo ${a[*]}
1 3 4 5
 
数组分片
a=(`seq 1 5`)
echo ${a[@]:0:3}
123
 
 
数组替换
a=(1 2 3 4 5)
echo ${a[@]/3/100}
1 2 100 4 5
---------------------------------------
if逻辑判断
#!/bin/bash
read -p "please input your score:" a
if ((a<60));then
echo "You didn‘t pass the exam."
else
echo "Good! You passed the exam."
fi
 
-------------------------------------------------------------------------------------------------------
带有elif
格式:
if 判断语句一;then
command
elif 判断语句二;then
command
else
command
fi
 
例如:
#!/bin/bash
read -p "please input your score: " a
if ((a<60));then
echo "You didn‘t pass the exam"
elif((a>=60)) && ((a<85));then
echo "Good! You pass the exam"
else
echo "very good! Your socre is very high!"
fi
--------------------------------------------------------------------------------------------------------
if ls /dev/sdb1 > /dev/null 2> /dev/null ; then 
判断盘符

shell脚本编程

标签:ash   变量   开头   bin   val   数组元素   格式   时间   介绍   

原文地址:http://www.cnblogs.com/a12a/p/6692944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!