- 浏览: 249290 次
- 性别:
- 来自: 济南
文章分类
- 全部博客 (303)
- c (31)
- c++ (16)
- java (18)
- c# (1)
- python (3)
- java web (6)
- oracle (7)
- sqlserver (2)
- mysql (2)
- android (24)
- android系统 (15)
- android多媒体部分 (15)
- android游戏 (12)
- linux (26)
- javaScript (1)
- ajax (1)
- node JS (2)
- html (5)
- apache (3)
- jboss (1)
- weblogic (0)
- 通信协议 (10)
- 云计算 (1)
- 分布式 (5)
- ejb (1)
- webservice (5)
- 设计模式 (16)
- JNI (6)
- swing (13)
- 版本控制 (1)
- UML (1)
- xml (4)
- spring (5)
- hibernate (5)
- struts1 (3)
- struts2 (4)
- ibatis (0)
- tomcat (2)
- 心得体会 (1)
- css (1)
- 嵌入式 (41)
- arm体系结构 (10)
流程控制
退出状态
有四种退出状态
最后命令退出状态$?
控制次序命令$$ ||
处理shell脚步本退出或shell退出及相就退出状态或函数返回码
退出当前进程
exit n
n为数字
流控制
if then else
格式为:
if 条件1
then 命令1
elif 条件2
then 命令2
else 命令3
fi
简单的if 语句
if条件
then 命令
fi
示例代码:测试1是否小于2
#!/bin/sh
#test
#测试1是否小于2
if [ "1" -lt "2" ]
then
#1小于2
echo "yes,1 is less than 2"
fi
修改文件权限
chmod u+x test
执行角本
[retacn@localhost tmp]$ ./test
yes,1 is less than 2
变量值测试
示例代码如下:用户键入return键后变量name是否包含信息
#!/bin/sh
#test2
#测试用户键入return键后name中是否包含信息
echo "Enter your name:"
read NAME
#
if [ "$NAME" = "" ]
then
echo "You must enter some information"
fi
运行角本:
[retacn@localhost tmp]$ ./test2
Enter your name:
You must enter some information
文件考贝输出检查
#!/bin/sh
#test3
#测试文件拷贝是否正常
if cp myfile myfile.bak
then
echo "copy success"
else
echo "`basename $0`:error could not copy the files" >&2
fi
执行结果:
[retacn@localhost tmp]$ ls
myfile1 test test2 test3
[retacn@localhost tmp]$ ./test3
cp: 无法 stat “myfile”: 没有那个文件或目录
test3:error could not copy the files
不输出系统错误信息
if cp myfile myfile.bak >/dev/null 2>
then ...
当前目录测试
示例代码如下:检查当前目录是否为根目录
#!/bin/sh
#test4
#检查当前目录是否是根目录
DIRECTORY=`pwd`
#
if [ "$DIRECTORY" != "/" ];then
#it is not the root directory,show the error message
echo "You need to be in the root directory not $DIRECTORY to run this
script" >&2
#退出返回错误值1
exit 1
fi
执行结果:
[retacn@localhost tmp]$ chmod u+x test4
[retacn@localhost tmp]$ ./test4
You need to be in the root directory not /home/retacn/tmp to run this
script
文件权限测试
示例代码如下:测试文件 test.txt是否被设置到LOGNAME
#!/bin/sh
#test5
#
LOGFILE=test.txt
echo $LOGFILE
if [ ! -w "#LOGFILE" ];then
echo "you can not write to $LOGFILE" >&2
fi
[retacn@localhost tmp]$ ./test5
test.txt
you can not write to test.txt
测试传递到角本中的参数
位置参数
$0 是角本的名称
$1 第一个参数
$2 第二个参数
以此类推
在$9之后的参数要用括号括起来,如${10}
$# 参数的个数
$* 和$@ 表示所有参数
示例代码如下
#!/bin/sh
#test5
#测试传入角本的参数个数
if [ $# -lt 3 ]; then
#小于三个参数
echo "usage: `basename $0`arg1 arg2 arg3" >&2
exit 1
fi
echo "arg1: $1"
echo "arg2: $2"
echo "arg3: $3"
执行结果
输入一个参数
[retacn@localhost tmp]$ ./test6 cup yue
usage: test6arg1 arg2 arg3
输入三个参数
[retacn@localhost tmp]$ ./test6 cup yue zhen hua
arg1: cup
arg2: yue
arg3: zhen
决定角本是否为交互模式
角本的运行
交互模式(终端模式)
非交互模式(cron或at)
示例代码如下:
#!/bin/sh
#test7
#检查角本是否是交互模式,返回1为交互
if [ -t ]; then
echo "we are interactive with terminal"
else
echo "we must be running from some background process probably
cron or at"
fi
执行结果:
[retacn@localhost tmp]$ ./test7
we are interactive with terminal
检测运行角本的用户
示例代码如下:
#!/bin/sh
#test8
#检测执行角本的用户
if [ "$LOGNAME" != "root" ]
then
echo "you need to root to run this script" >&2
exit 1
else
echo "yes indeed you are $LOGNAME proceed"
fi
执行结果
[retacn@localhost tmp]$ chmod u+x test8
[retacn@localhost tmp]$ ./test8
you need to root to run this script
[retacn@localhost tmp]$ su - root
口令:
[root@localhost ~]# cd /home/retacn/tmp/
[root@localhost tmp]# ./test8
yes indeed you are root proceed
将角本参数传入系统命令
#!/bin/sh
#test9
#传入系统命令
DIRECTORY=$1
if [ "`ls -A $DIRECTORY`" = "" ]; then
echo "$DIRECTORY is indeed empty"
else
echo "$DIRECTORY is not empty"
fi
执行结果:
[root@localhost tmp]# ls
myfile1 test2 test4 test6 test8 test.txt
test test3 test5 test7 test9
[root@localhost tmp]# ./test9 test.txt
test.txt is not empty
null:命令用法
示例代码
#!/bin/sh
#test9
#传入系统命令
DIRECTORY=$1
if [ "`ls -A $DIRECTORY`" = "" ]; then
echo "$DIRECTORY is indeed empty"
else :
fi
测试目录创建结果
示例代码如下:
#!/bin/sh
#test10
#测试目录创建目结果
DIRECTORY=$1
#检查字符串是否为空
if [ "$DIRECTORY" = "" ]
then
echo "usage:`basename $0` directory to create " >&2
fi
#如果目录已存在OB
if [ -d $DIRECTORY ]
then : #不做操作
else
#提示用户是否创建目录
echo "the directory does exist"
echo -n "create it now? [y..n]:"
read ANS
if [ "$ANS" = "y" ] || [ "$ANS" = "Y" ]
then
echo "creating now"
#创建目录
mkdir $DIRECTORY >/dev/null 2>&1
exit 1
#检测是否创建成功
if [ $? != 0]; then
echo "error creating the directory $DIRECTORY" >&2
exit 1
fi
else :
fi
fi
执行结果:
[root@localhost tmp]# chmod u+x test10
[root@localhost tmp]# ./test10
usage:test10 directory to create
[root@localhost tmp]# ./test10 temp
the directory does exist
create it now? [y..n]:y
creating now
[root@localhost tmp]# ls
myfile1 test test2 test4 test6 test8 test.txt
temp test10 test3 test5 test7 test9
发表评论
-
u-boot Makefile 文件分析
2013-06-01 21:44 2372Makefile文件分析 # #(C)Copyri ... -
uboot start.S文件分析
2013-06-03 22:18 1285U-boot第一个开始文件arch\arm\cpu\arm1 ... -
u-boot mkconfig文件分析
2013-05-31 21:29 1107Mkconfig文件分析 #!/bin/ ... -
链接地址学习笔记
2013-05-05 12:40 1238链接地址 启动过程 示例代码如下: ... -
DDR学习笔记
2013-05-11 14:19 999DDR 15条地址线32k 128M*2(20)=2(2 ... -
nand flash学习笔记一
2013-05-13 21:05 930Nandflash 原理图上有data0-data7 ... -
openJTAG学习笔记一
2013-05-22 21:45 2130安装软件 光盘Windows\install目录下的 01.O ... -
linux进程管理学习笔记
2013-03-28 20:57 1295linux 进程管理 1 linux进程控制 进程的四个要素: ... -
字符设备驱动程序学习笔记一
2013-04-01 21:55 827linux 驱动程序 字符设备驱动程序 网络接口驱动程序 块设 ... -
字符设备驱动程序学习笔记二
2013-04-04 10:29 715字符驱动程序 1 设备号 字符设备通过字符设备文件来存取 ls ... -
字符设备驱动程序学习笔记三
2013-04-04 14:03 728memdev.h文件示例代码如下: #ifndef _MEM ... -
字符设备驱动程序学习笔记四
2013-04-05 11:12 555竟争与互斥 程序调试 1 ... -
GPIO学习笔记
2013-04-14 19:50 770用汇编点亮一个led 1看原理图GPK4=0,led亮G ... -
系统时钟学习笔记
2013-05-04 21:59 80212m晶振----->pll------>cpu ... -
UART学习笔记
2013-05-04 22:00 1098串口(UART) DIV_VAL=(PCLK/(bpsx1 ... -
linux内存管理学习笔记
2013-03-12 20:50 9981 linux内存管理 地址类型 物理地址 出现在cpu地址 ... -
嵌入式linux系统学习笔记
2013-03-06 21:39 896嵌入式linux内核制作 1 清除原有配置文件与中间文件 x8 ... -
原理图学习笔记一
2013-02-17 22:24 368画个草图也挺过瘾 -
进程间通信学习笔记一(管道通信)
2013-02-01 20:08 1362进程间通信(ipc) 应用场景: 数据传输 资源共享 通知事件 ... -
进程间通信学习笔记二(信号通信)
2013-02-16 21:39 731信号通信 用户按某些键时,产生信号 硬件异常产生信号 进程用k ...
相关推荐
变量: 变量的赋值和引用 在shell编程中变量没有类型,简称弱类型编程语言,不需要声明,在引用这个变量时会创建它(在变量名前加$符号引用变量的值)。在定义变量时,若String中包含空格、制表符、换行符,需用单...
非常详细的Linux_shell编程学习笔记,能帮助到初学者增加理解。
shell编程-shell编程-分支语句(2) shell编程-shell编程-循环语句(1) shell编程-shell编程-循环语句(2) Linux的shell编程(一) Linux的shell编程(二) Linux的shell编程(三) Linux的shell编程(四) ...
该文档是本人阅读《linux命令行大全》对最后第四章节的shell脚本编程的学习笔记总结。可以在只看次笔记的情况下完全掌握shell编程
shell script学习中的笔记
「linuxshell编程学习笔记shell运算符号和运算命令」.docx
linuxshell编程学习笔记(5)shell运算符号和运算命令.pdf
本文由TinyLab.org原创。 这里只是个人学习笔记,主要包括Shell概述、Shell变量、位置参数、特殊符号、别名、各种控制语句、函数等Shell编程知识。
学习笔记:LINUX与UNIX_Shell编程指南
Shell编程笔记,有很多关于Shell编程的干货,适合学习
这是因为,在 Unix 中,shell 可不是简单的命令解释器(典型的有 Windows 中的 DOS ),而是一个全功能的编程环境。 这并不意味着 shell 非常容易学通,您知道,好事多磨,这还是要花点工夫的。;-) 但请相信我,这...
学习shell编程的资料
这次难得有时间和条件来学习,所以做好笔记。 一:shell 能做什么 二:shell 脚本的几种执行方式,我们创建文件 start.sh,并且赋予 执行权限 chomd +x start.sh #然后可以通过以下命令执行 #相对路径,或者绝对...
这是我的shell编程笔记,我有很多自己的学习笔记,供我以及大家复习使用,还有其他的哦 C语言笔记,java笔记,网页笔记,javaweb笔记,gcc笔记,等,我会慢慢传
。。。
。。。
提供unix shell脚本的编程指南,本书包含shell编程的经典开发实例
Linux Shell编程笔记,详情可以参考文档CSDN Linux 学习之路专栏http://blog.csdn.net/column/details/wgb-linux.html
shell编程,写的比较好的基础入门资源