- 浏览: 679922 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
lanshui777:
压缩文件里面是空的....
Chrome扩展开发教程 -
seraph炽:
成功!多谢
windows 运行java出现错误:Could not find Java SE Runtime Environment -
canlynet:
应该是国外的网站,被过滤了无法访问。你百度一下就可以看到很多教 ...
python unittest用法 -
草原狙击手:
http://pyunit.sourceforge.net/p ...
python unittest用法 -
fantaxy025025:
现在可以了。thanks!lllt 写道这位哥们,这个写法根本 ...
表格中强制换行和强制不换行(兼容firefox)
--------------------------------------- 【Shell编程笔记】 ---------------------------------------
【1】变量
i=1 #等号两边不能有空格,所有变量只有一种类型——字符窜
echo $i # 引用变量要用$符号,如果变量不止由字母数字下划线组成,还需要加{},如${A[0]}
【2】 echo `date '+%Y-%m-%d %H:%M:%S'` # 后置引用,将date输出返回
【3】 : # 空命令,等价于NOP,也可认为与true相同,用途如下面:
while : # 同while true,死循环
do
...
done
if :l # 等价于if [ 1 ],等价于if test 1
then :
else
take-some-action
fi
: > filename #创建空文件或清空文件,同cat /dev/null > filename但不会产生新进程
【4】let执行整数运算
i=1
j=2
let "k=i+j"
((k=i+j))
k=$i+$j
【5】 格式化数字01,02,...
num=`printf "%02d" 1`
echo $num
==============================================================
【6】 test的用法 等价于[ contitions ]
1)判断表达式
test 表达式为真 等价于if test ! 表达式为假
test 表达式1 –a 表达式2 两个表达式都为真
test 表达式1 –o 表达式2 两个表达式有一个为真
2)判断字符串
test –n 字符串 字符串的长度非零
test –z 字符串 字符串的长度为零
test 字符串1 = 字符串2 字符串相等
test 字符串1 != 字符串2 字符串不等
3)判断整数
test 整数1 –eq 整数2 整数相等(equal)
test 整数1 –ge 整数2 整数1大于等于整数2 (greater or equal)
test 整数1 –gt 整数2 整数1大于整数2 (greater than)
test 整数1 –le 整数2 整数1小于等于整数2 (less or equal)
test 整数1 –lt 整数2 整数1小于整数2 (less than)
test 整数1 –ne 整数2 整数1不等于整数2 (not equal)
4)判断文件
test File1 –ef File2 两个文件具有同样的设备号和i结点号
test File1 –nt File2 文件1比文件2 新
test File1 –ot File2 文件1比文件2 旧
test –b File 文件存在并且是块设备文件
test –c File 文件存在并且是字符设备文件
test –d File 文件存在并且是目录
test –e File 文件存在
test –f File 文件存在并且是正规文件
test –g File 文件存在并且是设置了组ID
test –G File 文件存在并且属于有效组ID
test –h File 文件存在并且是一个符号链接(同-L)
test –k File 文件存在并且设置了sticky位
test –b File 文件存在并且是块设备文件
test –L File 文件存在并且是一个符号链接(同-h)
test –o File 文件存在并且属于有效用户ID
test –p File 文件存在并且是一个命名管道
test –r File 文件存在并且可读
test –s File 文件存在并且是一个套接字
test –t FD 文件描述符是在一个终端打开的
test –u File 文件存在并且设置了它的set-user-id位
test –w File 文件存在并且可写
test –x File 文件存在并且可执行
==============================================================
【7】数组
(1) name = (value1 ... valuen) 此时下标从0开始
(2) name[index] = value
数组下标的范围没有任何限制,同时也不必使用连续的分量.
$ A=(a b c def)
==================================================
$ echo ${A[@]} //取全部元素
a b c def
==================================================
$ echo ${A[0]} //取第一个元素,等价于echo $A
a
==================================================
# 取得数组元素的个数
$ echo ${#A[@]}
4
$ echo ${#A}
4
$ echo ${#A[3]} //取得元素3即def的长度
3
==================================================
$ A[3]=yaoshuyin //将第三个元素重新赋值
$ echo ${A[@]}
a b c yaoshuyin
==================================================
//清除变量
$ unset A
$ echo ${A[@]}
$
=============示例 while循环===========
#建立数组
arrSource=("arrJobs.php" "arrSubHangye.php" "arrFirst.php" )
arrDest=("buildhr" \
"buildtrain/htdocs" \
"bankhr" \
"healthr" \
"elehr" \
)
#取数组无元素个数
lenArrSource=${#arrSource}
lenArrDest=${#arrDest}
#循环列出数组元素
i=0
while [ $i -lt $lenArrSource ]
do
echo ${arrSource[$i]}
let i++
done
i=0
while [ $i -lt $lenArrDest ]
do
echo ${arrDest[$i]}
let i++
done
==============示例: for循环==============
#源文件
arrSource=("/home/800hr/htdocs/login_jump.php")
#目标网站
arrDest=(ithr elehr buildhr bankhr healthr ctvhr chenhr mechr clothr cneduhr 56hr tourhr foodhr greenhr cnlawhr waimaohr)
for outer in ${arrSource} #${arrSource[*]} 是数组中的所有元素
do
for inner in ${arrDest}
do
echo "ln -s $outer /home/${inner}/campus/"
done
done
==============================================================
【8】预留变量
$# 位置参数的数量
$* 所有位置参数的内容,同$@
$? 命令执行后返回的状态
$$ 当前进程的进程号
$! 后台运行的最后一个进程号
$0 当前执行的进程名
$- SHELL的当前选项,和set相同
==============================================================
【9】控制语句
条件测试有:[[]],[],test 这几种,注意:[[]] 与变量之间用空格分开。
1)条件执行
if conditions; then
elif conditions; then
else
fi
for
2)条件执行
case 变量 in
值1)
语句...
;;
值2)
语句...
;;
...
值n)
语句...
;;
*) #其它情况
语句...
;;
esac
3)for循环
for i in $(seq 10); do
echo $i; # 输出1 2 3 4 5 6 7 8 9 10 不换行
done;
for((i=1;i<=10;i++));do
echo -n "$i "; # 输出1 2 3 4 5 6 7 8 9 10 不换行
done;
4)while循环
i=10;
while [[ $i -gt 5 ]];do
echo $i;
((i--));
done;
while read line; do
echo $line;
done < /etc/hosts; # 按行读取/etc/hosts内容并输出,同cat /etc/hosts
5)until循环(条件与while相反)
a=10;
until [[ $a -le 0 ]]; do # 同while [[ $a -gt 0 ]]; do
echo $a;
((a--));
done;
6)select选择语句
供用户选择选项,用case语句判断执行不同的语句。典型菜单。
select ch in "begin" "end" "exit"
do
case $ch in
"begin")
echo "start something"
;;
"end")
echo "stop something"
;;
"exit")
echo "exit"
break;
;;
*)
echo "Ignorant"
;;
esac
done;
执行效果如下(?后面是用户输入选择):
1) begin
2) end
3) exit
#? 1
start something
#? 2
stop something
#? 4
Ignorant
#? 6
Ignorant
#? 3
exit
【1】变量
i=1 #等号两边不能有空格,所有变量只有一种类型——字符窜
echo $i # 引用变量要用$符号,如果变量不止由字母数字下划线组成,还需要加{},如${A[0]}
【2】 echo `date '+%Y-%m-%d %H:%M:%S'` # 后置引用,将date输出返回
【3】 : # 空命令,等价于NOP,也可认为与true相同,用途如下面:
while : # 同while true,死循环
do
...
done
if :l # 等价于if [ 1 ],等价于if test 1
then :
else
take-some-action
fi
: > filename #创建空文件或清空文件,同cat /dev/null > filename但不会产生新进程
【4】let执行整数运算
i=1
j=2
let "k=i+j"
((k=i+j))
k=$i+$j
【5】 格式化数字01,02,...
num=`printf "%02d" 1`
echo $num
==============================================================
【6】 test的用法 等价于[ contitions ]
1)判断表达式
test 表达式为真 等价于if test ! 表达式为假
test 表达式1 –a 表达式2 两个表达式都为真
test 表达式1 –o 表达式2 两个表达式有一个为真
2)判断字符串
test –n 字符串 字符串的长度非零
test –z 字符串 字符串的长度为零
test 字符串1 = 字符串2 字符串相等
test 字符串1 != 字符串2 字符串不等
3)判断整数
test 整数1 –eq 整数2 整数相等(equal)
test 整数1 –ge 整数2 整数1大于等于整数2 (greater or equal)
test 整数1 –gt 整数2 整数1大于整数2 (greater than)
test 整数1 –le 整数2 整数1小于等于整数2 (less or equal)
test 整数1 –lt 整数2 整数1小于整数2 (less than)
test 整数1 –ne 整数2 整数1不等于整数2 (not equal)
4)判断文件
test File1 –ef File2 两个文件具有同样的设备号和i结点号
test File1 –nt File2 文件1比文件2 新
test File1 –ot File2 文件1比文件2 旧
test –b File 文件存在并且是块设备文件
test –c File 文件存在并且是字符设备文件
test –d File 文件存在并且是目录
test –e File 文件存在
test –f File 文件存在并且是正规文件
test –g File 文件存在并且是设置了组ID
test –G File 文件存在并且属于有效组ID
test –h File 文件存在并且是一个符号链接(同-L)
test –k File 文件存在并且设置了sticky位
test –b File 文件存在并且是块设备文件
test –L File 文件存在并且是一个符号链接(同-h)
test –o File 文件存在并且属于有效用户ID
test –p File 文件存在并且是一个命名管道
test –r File 文件存在并且可读
test –s File 文件存在并且是一个套接字
test –t FD 文件描述符是在一个终端打开的
test –u File 文件存在并且设置了它的set-user-id位
test –w File 文件存在并且可写
test –x File 文件存在并且可执行
==============================================================
【7】数组
(1) name = (value1 ... valuen) 此时下标从0开始
(2) name[index] = value
数组下标的范围没有任何限制,同时也不必使用连续的分量.
$ A=(a b c def)
==================================================
$ echo ${A[@]} //取全部元素
a b c def
==================================================
$ echo ${A[0]} //取第一个元素,等价于echo $A
a
==================================================
# 取得数组元素的个数
$ echo ${#A[@]}
4
$ echo ${#A
$ echo ${#A[3]} //取得元素3即def的长度
3
==================================================
$ A[3]=yaoshuyin //将第三个元素重新赋值
$ echo ${A[@]}
a b c yaoshuyin
==================================================
//清除变量
$ unset A
$ echo ${A[@]}
$
=============示例 while循环===========
#建立数组
arrSource=("arrJobs.php" "arrSubHangye.php" "arrFirst.php" )
arrDest=("buildhr" \
"buildtrain/htdocs" \
"bankhr" \
"healthr" \
"elehr" \
)
#取数组无元素个数
lenArrSource=${#arrSource
#循环列出数组元素
i=0
while [ $i -lt $lenArrSource ]
do
echo ${arrSource[$i]}
let i++
done
i=0
while [ $i -lt $lenArrDest ]
do
echo ${arrDest[$i]}
let i++
done
==============示例: for循环==============
#源文件
arrSource=("/home/800hr/htdocs/login_jump.php")
#目标网站
arrDest=(ithr elehr buildhr bankhr healthr ctvhr chenhr mechr clothr cneduhr 56hr tourhr foodhr greenhr cnlawhr waimaohr)
for outer in ${arrSource
for inner in ${arrDest
echo "ln -s $outer /home/${inner}/campus/"
done
done
==============================================================
【8】预留变量
$# 位置参数的数量
$* 所有位置参数的内容,同$@
$? 命令执行后返回的状态
$$ 当前进程的进程号
$! 后台运行的最后一个进程号
$0 当前执行的进程名
$- SHELL的当前选项,和set相同
==============================================================
【9】控制语句
条件测试有:[[]],[],test 这几种,注意:[[]] 与变量之间用空格分开。
1)条件执行
if conditions; then
elif conditions; then
else
fi
for
2)条件执行
case 变量 in
值1)
语句...
;;
值2)
语句...
;;
...
值n)
语句...
;;
*) #其它情况
语句...
;;
esac
3)for循环
for i in $(seq 10); do
echo $i; # 输出1 2 3 4 5 6 7 8 9 10 不换行
done;
for((i=1;i<=10;i++));do
echo -n "$i "; # 输出1 2 3 4 5 6 7 8 9 10 不换行
done;
4)while循环
i=10;
while [[ $i -gt 5 ]];do
echo $i;
((i--));
done;
while read line; do
echo $line;
done < /etc/hosts; # 按行读取/etc/hosts内容并输出,同cat /etc/hosts
5)until循环(条件与while相反)
a=10;
until [[ $a -le 0 ]]; do # 同while [[ $a -gt 0 ]]; do
echo $a;
((a--));
done;
6)select选择语句
供用户选择选项,用case语句判断执行不同的语句。典型菜单。
select ch in "begin" "end" "exit"
do
case $ch in
"begin")
echo "start something"
;;
"end")
echo "stop something"
;;
"exit")
echo "exit"
break;
;;
*)
echo "Ignorant"
;;
esac
done;
执行效果如下(?后面是用户输入选择):
1) begin
2) end
3) exit
#? 1
start something
#? 2
stop something
#? 4
Ignorant
#? 6
Ignorant
#? 3
exit
发表评论
-
Linux安装扩展源epel并更新证书
2019-05-14 15:33 579今天更新linux上的软件hddtemp发现一台centos ... -
nodejs安装、模块安装、以及forever让脚本后台运行
2019-02-28 08:40 647centos 安装nodejs: yum install ... -
Linux LVS NAT模式
2019-01-15 14:14 401Linux下使用LVS配置负载均衡十分方便,本文只介绍NA ... -
Centos7 firewalld常用命令
2019-01-10 14:05 520CentOS7使用firewalld代替了iptables ... -
mongodb副本集配置
2019-01-10 09:20 499mongodb的副本集可以很方便的做到数据实时同步到所有主 ... -
使用sync和sesync2进行服务器文件实时同步
2019-01-09 17:00 667主服务器: 192.168.1.8备份服务器: 192.16 ... -
我的firewalld常用命令
2018-12-03 15:12 353添加 firewall-cmd --zone=publ ... -
Linux ssh免密码登录
2017-12-28 14:29 451本机:192.168.1.228 要免密码登陆的远程主机: ... -
Imagemagick转换图片成ico
2017-07-09 06:20 958Imagemagick是linux、windows、mac下都 ... -
我的.vimrc配置文件
2017-05-13 21:49 465我的.vimrc配置文件 写道 "set mo ... -
Linux中vim的使用
2017-05-13 21:45 872维护linux服务器的时候,大部分时间都在安装服务器端的各种 ... -
linux中利用Libreoffice可将office文档(doc,ppt,xls)转pdf文件
2017-03-18 08:19 3602在有些项目中,或许用户希望上传的word文档,excel表 ... -
centos安装ImageMagick和php的imagick扩展
2017-01-30 18:32 759ImageMagick下载地址: http://www. ... -
mysqld找回消失的InnoDB引擎
2017-01-30 17:01 536闲来无事,在linux服务器上编译安装mysql,成功后停掉 ... -
centos6编译安装apache和php-fpm以及扩展
2017-01-30 10:17 1596首先要下载到apache的源码包,这里我选择下载的是ht ... -
libreoffice将ppt、xls、doc等office文件转换为pdf
2017-01-28 20:20 1940首先要安装libreoffice软件: http://z ... -
centos6 安装或升级svn1.8
2016-12-22 22:19 744参考网址:http://blog.csdn.net/sam ... -
php编译选项
2016-10-31 16:56 391yum install -y pam-devel libc- ... -
git clone的错误fatal: HTTP request failed解决办法
2016-06-24 10:56 5985git clone时可能发生错误,提示:fatal: HT ... -
linux上快速拷贝大量小文件
2015-06-19 17:22 1552将机器A上的web目录上传到到机器 B(192.168.0 ...
相关推荐
Linux Shell编程笔记,详情可以参考文档CSDN Linux 学习之路专栏http://blog.csdn.net/column/details/wgb-linux.html
awk基础入门(4) ...Linux的shell编程(一) Linux的shell编程(二) Linux的shell编程(三) Linux的shell编程(四) Shell学习笔记 一 Shell学习笔记 二 Shell学习笔记 三 Shell学习笔记 四 Shell学习笔记 五
《LINUX与UNIX SHELL编程指南》读书笔记-二次发布版
「linuxshell编程学习笔记shell运算符号和运算命令」.docx
linuxshell编程学习笔记(5)shell运算符号和运算命令.pdf
学习笔记:LINUX与UNIX_Shell编程指南
《LINUX与UNIX SHELL编程指南》读书笔记.pdf
非常详细的Linux_shell编程学习笔记,能帮助到初学者增加理解。
牛人写的笔记,希望能够给需要的人一些帮助,主要是linux Unix 的shell编程笔记,有助于linux应用以及嵌入式开发人员参考
本书重点讲述了当下最流行的两个Linux Shell——Borne Again Shell (bash)和TC Shell(tcsh),从它们的新特性、增加和内建的插件讲起直至具体的应用。通过对一个个实例的讲解,循序渐进地介绍了Linux Shell中的各个...
一本关于linux与UNIX的shell脚本编程书籍,很不错,实用
学习shell编程的资料
LINUX操作命令与UNIX SHELL编程指南
要介绍了shell的相关知识教程,如shell编程需要的基础知识储备、shell脚本概念介绍及第一个脚本案例、shell脚本基础知识-作用地位-比其他语言优势、建立一个规范的shell脚本、执行shell脚本的多种方法及重要区别精讲...
本人整理的Unix&Linux Shell编程指南及学习笔记,个人觉得是学习shell不可多得的好资料!
该文档是本人阅读《linux命令行大全》对最后第四章节的shell脚本编程的学习笔记总结。可以在只看次笔记的情况下完全掌握shell编程
shell编程;精化笔记,总结,版式整齐,适合了解linux, 拓宽知识面,快来下载吧!
Linux内核学习笔记:内核同步.docx,Linux内核学习笔记:内核同步.docx,Linux内核学习笔记:内核同步.docx