仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1297|回复: 9
打印 上一主题 下一主题

[shell编程] 给大家带来Shell剧本编程基本

[复制链接]
蒙在股里 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-14 20:22:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的小伙伴们!Shell简介
Shell复杂的说就是下令剖析器,将用户输出的指令转换为响应的呆板可以实行的步伐。
Shell剧本是一个包括一系列下令序列的文本文件(批处置)。当运转这个剧本文件时,文件中包括的下令序列将失掉实行。
HelloWorld
Shell剧本的第一行必需是以下格局:#!/bin/bash
标记#!用来指定该剧本文件的剖析步伐。例中利用bash,也能够利用其他shell。如#!/bin/sh。
当编纂好剧本后,必需使其具有可实行属性。chmod+xfilename
Shell剧本hello.sh
viewplaincopytoclipboardprint?
#!/bin/bash
echo"helloworld!"
mkdir./helloworld
#!/bin/bash
echo"helloworld!"
mkdir./helloworld
Shell中的变量
在Shell编程中,一切的变量都由字符串构成,其实不必要事后对变量声明。
Shell剧本s1.sh
viewplaincopytoclipboardprint?
#!/bin/sh
#号前面是正文
#setvariablea
a="hello"
#printa
echo"Ais:$a"
#!/bin/sh
#号前面是正文
#setvariablea
a="hello"
#printa
echo"Ais:$a"
下令行参数传送
$#:传进剧本的下令行参数个数
$*:一切的下令行参数,各参数值之间留有空格
$0:下令自己(shell文件名)
$1:第一个下令行参数
$2:第二个下令行参数
shell剧本s2.sh
viewplaincopytoclipboardprint?
#!/bin/sh
echo"numerofvars:"$#
echo"valuesofvars:"$*
echo"valueofvar1:"$1
echo"valueofvar2:"$2
echo"valueofvar3:"$3
echo"valueofvar4:"$4
#!/bin/sh
echo"numerofvars:"$#
echo"valuesofvars:"$*
echo"valueofvar1:"$1
echo"valueofvar2:"$2
echo"valueofvar3:"$3
echo"valueofvar4:"$4
运转./s2.shabcd
输入了局:
numerofvars:4
valuesofvars:abcd
valueofvar1:a
valueofvar2:b
valueofvar3:c
valueofvar4:d
Shell中的部分变量
shell剧本s3.sh
viewplaincopytoclipboardprint?
#!/bin/bash
hello="var1"
echo$hello
functionfuncl
{
localhello="var2"
echo$hello
}
funcl
echo$hello
#!/bin/bash
hello="var1"
echo$hello
functionfuncl
{
localhello="var2"
echo$hello
}
funcl
echo$hello
在变量初次被赋值时加上local关头字能够声明一个部分变量
注重:(1)变量赋值时,“=”摆布双方都不克不及有空格
(2)BASH中的语句开头不必要分号
Shell中的把持布局
if语句
if[expression]
then
#codeblock
if
if[expression]
then
#codeblock
else
#codeblock
fi
对照运算符
对照操纵整数操纵字符串操纵对照操纵整数操纵字符串操纵
不异-eq=年夜于或即是-ge
分歧-ne!=小于或即是-le
年夜于-gt>为空-z
小于-lt<不为空-n

利用实例:
对照整数a是不是年夜于整数b:if[$a-gt$b]判别字符串a是不是为空:if[-z$a]
注重:
(1)在"["和"]"标记的摆布都留有空格(2)"="摆布都有空格
shell剧本s4.sh
viewplaincopytoclipboardprint?
#!/bin/bash
a=$1
b=$2
if[-z$a]||[-z$b]
then
echo"pleaseenter2no"
exit1
fi
if[$a-eq$b];then
echo"numbera=numberb"
elseif[$a-gt$b]
then
echo"numbera>numberb"
elif[$a-lt$b]
then
echo"numbera<numberb"
fi
fi
#!/bin/bash
a=$1
b=$2
if[-z$a]||[-z$b]
then
echo"pleaseenter2no"
exit1
fi
if[$a-eq$b];then
echo"numbera=numberb"
elseif[$a-gt$b]
then
echo"numbera>numberb"
elif[$a-lt$b]
then
echo"numbera<numberb"
fi
fi
判别
-e文件已存在-f文件是一般文件-s文件巨细不为零-d文件是一个目次
-r文件对以后用户能够读取-w文件对以后用户能够写进-x文件对以后用户能够实行
shell剧本s5.sh
viewplaincopytoclipboardprint?
#!/bin/sh
folder=/home
[-r"$folder"]&&echo"Canread$folder"
[-f"$folder"]||echo"thisisnotfile"
#!/bin/sh
folder=/home
[-r"$folder"]&&echo"Canread$folder"
[-f"$folder"]||echo"thisisnotfile"
shell剧本s6.sh
viewplaincopytoclipboardprint?
#!/bin/bash
DIR=$1
#ifthestringempty
if["$DIR"=""]
then
echo"usage:`basename$0`directorytocreate">&2
exit1
fi
echo"dir"$DIR
if[-d$DIR]
then
echo"Thedirectoryalreadyexist"
exit0
else
echo"Thedirectorydoesexist"
echo-n"Createisnow?[Y/N]:"
readcreate
if["$create"="y"]||["$create"="Y"]
then
echo"creatingnow"
if[mkdir$DIR]
DIR=""
fi

if["$DIR"=""]
then
echo"createdirectorysucess"
else
echo"createdirectoryerror"
fi
elif["$create"="n"]||["$create"="N"]
then
echo"doesnotcreatedirectory"
exit0
else
echo"Errorsorder"
exit1
fi
fi
#!/bin/bash
DIR=$1
#ifthestringempty
if["$DIR"=""]
then
echo"usage:`basename$0`directorytocreate">&2
exit1
fi
echo"dir"$DIR
if[-d$DIR]
then
echo"Thedirectoryalreadyexist"
exit0
else
echo"Thedirectorydoesexist"
echo-n"Createisnow?[Y/N]:"
readcreate
if["$create"="y"]||["$create"="Y"]
then
echo"creatingnow"
if[mkdir$DIR]
DIR=""
fi

if["$DIR"=""]
then
echo"createdirectorysucess"
else
echo"createdirectoryerror"
fi
elif["$create"="n"]||["$create"="N"]
then
echo"doesnotcreatedirectory"
exit0
else
echo"Errorsorder"
exit1
fi
fi
for轮回
for轮回布局与C言语中有所分歧,在bash中for轮回的基础布局式
forvarin
[list]
do
#codelock
done
个中$var是轮回把持变量,
[list]是var遍历的一个汇合,do/done对包括了轮回体。
别的,假如for和do写在统一行,必需在do后面加";"。
shell剧本s7.sh
viewplaincopytoclipboardprint?
#!/bin/bash
fordayinSunMonTueWedThuFriSat
do
echo$day
done
#!/bin/bash
fordayinSunMonTueWedThuFriSat
do
echo$day
done
shell剧本统计以后目次下的文件数
viewplaincopytoclipboardprint?
#!/bin/bash

counter=0
forfilesin*
do
counter=`expr$counter+1`
done
echo"Thereare$counterfilesin`pwd`weneedtoprocess"
#!/bin/bash
counter=0
forfilesin*
do
counter=`expr$counter+1`
done
echo"Thereare$counterfilesin`pwd`weneedtoprocess"
shell剧本将用户输出的数字按倒序的体例输入
viewplaincopytoclipboardprint?
#!/bin/bash
echo-n"Pleaswenternumber:"
readn
sd=0
rev=""
on=$n
echo"$n"
while[$n-gt0]
do
sd=$(($n%10))#getRemainder
n=$(($n/10))#getnextdigit
rev=$(echo$rev$sd)
done
echo"$oninareverseorder$rev"
#!/bin/bash
echo-n"Pleaswenternumber:"
readn
sd=0
rev=""
on=$n
echo"$n"
while[$n-gt0]
do
sd=$(($n%10))#getRemainder
n=$(($n/10))#getnextdigit
rev=$(echo$rev$sd)
done
echo"$oninareverseorder$rev"
until轮回
until轮回的基础布局
until[condition]
do
#codeblock
done
while和until的区分在于while是为真时实行,until是为假时实行。
shell剧本挪动一个文件,假如方针存在,监督该文件,直到文件被删除才挪动文件。
viewplaincopytoclipboardprint?
#!/bin/bash
if["$1"=""]||["$2"=""]
then
echo"Pleaseenterfilename"
exit1
fi
if[-e$2]
then
echo"Thefilealreadyexists"
until[!-f$2]
do
sleep1
done
fi
if[!`mv$1$2`]
then
echo"mvsucessful"
else
echo"mverror"
fi
#!/bin/bash
if["$1"=""]||["$2"=""]
then
echo"Pleaseenterfilename"
exit1
fi
if[-e$2]
then
echo"Thefilealreadyexists"
until[!-f$2]
do
sleep1
done
fi
if[!`mv$1$2`]
then
echo"mvsucessful"
else
echo"mverror"
fi

case语句
case语句布局
case"$var"in
condition1)
;;
condion2)
;;
*)
defaultstatments;;
esac
shell剧本判别键盘输出巨细写
viewplaincopytoclipboardprint?
#!/bin/bash
echo"Hitakey,thenhitreturn."
readKeypress
case"$Keypress"in
[A-Z])echo"Uppercaseletter";;
[a-z])echo"Lowercaseletter";;
[0-9])echo"Digit";;
*)echo"Punctuation,whitespace,orother";;
esac
#!/bin/bash
echo"Hitakey,thenhitreturn."
readKeypress
case"$Keypress"in
[A-Z])echo"Uppercaseletter";;
[a-z])echo"Lowercaseletter";;
[0-9])echo"Digit";;
*)echo"Punctuation,whitespace,orother";;
esac
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的好朋友们!
只想知道 该用户已被删除
沙发
发表于 2015-1-16 15:38:37 | 只看该作者

给大家带来Shell剧本编程基本

我们自学,就这个循环的过程中,我们学习了基本操作,用vi,shell,模拟内存的分配过程等一些OS管理。
小女巫 该用户已被删除
板凳
发表于 2015-1-19 06:28:51 | 只看该作者
随着实验课程的结束,理论课也该结束了,说实话教OS的这两位老师是我们遇到过的不错的老师(这话放这可能不太恰当).
莫相离 该用户已被删除
地板
发表于 2015-1-28 05:44:53 | 只看该作者
应对Linux的发展历史和特点有所了解,Linux是抢占式多任务多用户操作系统,Linux最大的优点在于其作为服务器的强大功能,同时支持多种应用程序及开发工具。
爱飞 该用户已被删除
5#
发表于 2015-2-5 18:37:44 | 只看该作者
在系统检测不到与Linux兼容的显卡,那么此次安装就可能不支持图形化界面安装,而只能用文本模式安装等等。
小魔女 该用户已被删除
6#
发表于 2015-2-13 06:25:36 | 只看该作者
Windows有MS-DOS?方式,在该方式下通过输入DOS命令来操作电脑;Linux与Windows类似,也有命令方式,Linux?启动后如果不执行?X-WINDOWS,就会处于命令方式下,必须发命令才能操作电脑。?
海妖 该用户已被删除
7#
发表于 2015-3-3 17:54:52 | 只看该作者
甚至目前许多应用软件都是基于它的。可是没有哪一个系统是十分完美的。
老尸 该用户已被删除
8#
发表于 2015-3-11 13:30:10 | 只看该作者
就这样,我们一边上OS理论课,一边上这个实验,这样挺互补的,老师讲课,一步一步地布置任务
小妖女 该用户已被删除
9#
发表于 2015-3-19 00:32:09 | 只看该作者
虽然大家都比较喜欢漂亮的mm,但是在学 linux 的过程中,还是要多和“男人”接触一下:P 遇到问题的时候,出来看说和上网查之外,就是要多用 linux 下的 man 命令找找帮助。
不帅 该用户已被删除
10#
发表于 2015-3-26 23:46:27 | 只看该作者
主流Linux发行版都自带非常详细的文档(包括手册页和FAQ),从系统安装到系统安全,针对不同层次的人的详尽文档,仔细阅读文档后40%问题都可在此解决。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-4-28 06:59

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表