执行脚本的几种方式:

1. sh a.sh 或者  bash a.sh  调用的是 /bin/bash 进程执行的,所以脚本不需要执行权限。

2. 直接使用绝对路径执行, /home/script/a.sh  ,脚本需要有执行权限,如果没有权限可执行 chmod a+x a.sh 加入执行权限。

(lampp启动数据库用的就是: /opt/lampp/lampp startmysql )

sh03.sh 根据时间创建目录

#!/bin/bash
echo -e "I will use 'touch' command to create 3 files."
read -p "Please input your filename:" fileuser filename=${fileuser:-"filename"} echo $filename date1=$(date -d '-2 day' +%Y%m%d) #前两天的日期
date2=$(date -d '-1 day' +%Y%m%d) #前一天的日期
date3=$(date "+%Y%m%d") #下面三行在配置文件名
file1=${filename}${date1}
file2=${filename}${date2}
file3=${filename}${date3} touch "$file1"
touch "$file2"
touch "$file3"

sh04.sh 数字运算

#!/bin/bash
echo -e "You SHOULD input 2 numbers,I will cross them!\n"
read -p "first number: " firstnu
read -p "second number: " secnu
total=$(($firstnu*$secnu))
echo -e "\n The result of $firstnu x $secnu is ==> $total"

sh05.sh 使用 test 判断文件存在和权限

echo -e "Please input a filename,I will check the filename's type and permission. \n\n"
read -p "Input a filename : " filename
test -z $filename && echo "You MUST input a filename." && exit test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit test -f $filename && filetype="regulare file"
test -d $filename && filetype="directory" test -r $filename && perm="readable"
test -w $filename && perm="$perm writable"
test -x $filename && perm="$perm executable" echo "The filename:$filename is a $filetype"
echo "And the permissions are : $perm"

sh06.sh 使用 [] 中括号做判断

read -p "Please input (Y/N):" yn
[ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK,continue" && exit
[ "$yn" == "N" -o "$yn" == "n" ] && echo "Oh,interrupt!" && exit
echo "I don't know what your choice is" && exit

sh07.sh 脚本传参

echo "The script name is ==> $0"
echo "Total parameter number is ==> $#"
[ "$#" -lt ] && echo "The number of parameter is less than 2. Stop here." && exit
echo "Your whole parameter is ==> '$@'"
echo "The 1st parameter ==> $1"
echo "The 2nd parameter ==> $2"

sh08.sh shift去除参数

echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'" shift # 进行第一次 "一个变量的 shift " echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'" shift # 进行第二次 "三个变量的 shift "
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"

sh06-2.sh if...then 单层判断

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

if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
echo "OK,continue"
exit
fi if [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
echo "Oh,interrupt!"
exit
fi echo "I don't know what your choice is" && exit

sh06-3.sh  if...elif...else...fi

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

if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
echo "OK,continue"
exit
elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
echo "Oh,interrupt!"
exit
else
echo "I don't know what your choice is" && exit
fi

sh09.sh

if [ "$1" == "hello" ]; then
echo "Hello,how ary you?"
elif [ "$1" == "" ]; then
echo "You MUST input parameters,ex> {$0 someword}"
else
echo "The only parameter is 'hello',ex> {$0 hello}"
fi

sh10.sh

echo "Now,I will detect your Linux server's services!"
echo -e "The www, ftp, ssh, and mail will be detect! \n" testing=$(netstat -tuln | grep ":80 ")
if [ "$testing" != "" ]; then
echo "WWW is running in your system."
fi
testing=$(netstat -tuln | grep ":22 ")
if [ "$testing" != "" ]; then
echo "SSH is running in your system."
fi
testing=$(netstat -tuln | grep ":21 ")
if [ "$testing" != "" ]; then
echo "FTP is running in your system."
fi
testing=$(netstat -tuln | grep ":25 ")
if [ "$testing" != "" ]; then
echo "Mail is running in your system."
fi

sh11.sh case...esac 判断

case $ in
"hello")
echo "Hello,how are you?"
;;
"")
echo "You MUST input parameters,ex> {$0 someword}"
;;
*)
echo "The only parameter is 'hello',ex> {$0 hello}"
;;
esac

sh12.sh

echo "This program will print your selection !"
read -p "Input your choice: " choice
case $choice in
#case $ in
"one")
echo "Your choice is ONE"
;;
"two")
echo "Your choice is TWO"
;;
"three")
echo "Your choice is THREE"
;;
*)
echo "Usage $0 {one|two|three}"
;;
esac

sh12-2.sh 使用function

function printit(){
echo -n "Your choice is " #加上 -n 可以不断行继续在同一行显示
}

echo "This program will print your selection !"
case $1 in
"one")
printit; echo $1 | tr 'a-z' 'A-Z' #将参数做大小写转换!
;;
"two")
printit; echo $1 | tr 'a-z' 'A-Z'
;;
"three")
printit; echo $1 | tr 'a-z' 'A-Z'
;;
*)
echo "Usage $0 {one|two|three}"
;;
esac

sh12-3.sh function带参数

function printit(){
echo -n "Your choice is $1" #加上 -n 可以不断行继续在同一行显示
} echo "This program will print your selection !"
case $ in
"one")
printit ;
;;
"two")
printit ;
;;
"three")
printit ;
;;
*)
echo "Usage $0 {one|two|three}"
;;
esac

sh13.sh while do done循环

  read -p "Please input yes/YES to stop this program:" yn
while [ "$yn" != "yes" -a "$yn" != "YES" ]
do
read -p "Please input yes/YES to stop this program:" yn
done echo "OK! you input the correct answer."

sh13-2.sh  until  do  done循环

until [ "$yn" == "yes" -o "$yn" == "YES" ]
do
read -p "Please input yes/YES to stop this program:" yn
done
echo "OK! you input the correct answer."

sh14.sh

s=
i=
while [ "$i" != "" ]
do
i=$(($i+)) #每次 i 都会增加1
s=$(($s+$i)) #每次都会累加一次!
done
echo "The result of '1+2+3+...+100' is ==> $s"

sh15.sh  for...do...done循环

for animal in dog cat elephant
do
echo "There are ${animal}s..."
done

sh16.sh

users=$(cut -d ':' -f1 /etc/passwd) #获取账号名称
for username in $users #开始循环进行
do
id $username
finger $username
done

sh17.sh  (该shell script需要检查,还有问题)

network="192.168.66" #先定义一个域的前面部分
for sitenu in $(seq ) #seq为sequence(连续)的缩写之意
do
#下面的语句取得ping 的回传值是正确的还是失败的
ping -c -w ${network}.${sitenu} &> /dev/null && result= || result=
#开始显示结果是正确的启动(UP) 还是错误的没有连通(DOWN)
if [ "$result" == ]; then
echo "Server ${network}.${sitenu} is UP."
else
echo "Server ${network}.${sitenu} is DOWN."
fi
done

检查网段IP使用情况

#!/bin/bash
#main---
network=192.168.
ping_count=
IP=
:>IP_use
:>IP_idle
:>ping_action
echo "`date "+%Y%m%d %H:%M:%S"`----->脚本开始执行......"
while [ $IP -lt ]
do
host=$network.$IP
echo "-------->开始检测$host服务器通迅是否正常,ping次数$ping_count."
ping $host -c $ping_count >.ping_tmp
sleep
cat .ping_tmp >>ping_action
echo "-------->服务器$host检测已完成."
sum_ping=`tail - .ping_tmp |head - |awk -F, '{print$2}' |cut -c -`
loss_ping=`tail - .ping_tmp |head - |awk -F, '{print$4}'|cut -c -`
if [ $sum_ping -eq $ping_count ];then
echo "-->$host IP 已经在使用中"
echo "-->$host IP 已经在使用中" >>IP_use
else
echo "$host IP 目前空闲:$loss_ping"
echo "$host IP 目前空闲" >>IP_idle
fi
IP=$((IP+))
done
echo "`date "+%Y%m%d %H:%M:%S"`----->脚本运行完毕......"

sh18.sh  遍历目录下文件的权限

#.先看看这个目录是否存在啊?
read -p "Please input a directory:" dir
if [ "$dir" == "" -o ! -d "$dir" ]; then
echo "The $dir is NOT exist in your system."
exit
fi #.开始测试文件。
filelist=$(ls $dir) #列出所有在该目录下的文件名
for filename in $filelist
do
perm=""
test -r "$dir/$filename" && perm="$perm readable"
test -w "$dir/$filename" && perm="$perm writable"
test -x "$dir/$filename" && perm="$perm executable"
echo "The file $dir/$filename's permission is $perm"
done

sh19.sh for...do...done 循环

read -p "Please input a number,I will count for 1+2+...+your_input:" nu
s=
for((i=;i<=$nu;i=i+))
do
s=$(($s+$i))
done
echo "The result of '1+2+3+...+$nu' is ==> $s"

shell script练习的更多相关文章

  1. shell及脚本4——shell script

    一.格式 1.1 开头 必须以 "# !/bin/bash"  开头,告诉系统这是一个bash shell脚本.注意#与!中间有空格. 二.语法 2.1 数值运算 可以用decla ...

  2. shell script

    一.shell script的编写与执行 1.shell script 的编写中还需要用到下面的注意事项: a.命令的执行是从上到下,从左到右地分析与执行 b.命令.参数间的多个空白都会被忽略掉 c. ...

  3. (copy) Shell Script to Check Linux System Health

    source: http://linoxide.com/linux-shell-script/shell-script-check-linux-system-health/ This article ...

  4. 这些年我们一起搞过的持续集成~Jenkins+Perl and Shell script

    这些年我们一起搞过的持续集成~Jenkins+Perl and Shell script ##转载注明出处:http://www.cnblogs.com/wade-xu/p/4378224.html ...

  5. CentOS Linux下一个tomcat起停,查看日志的shell script

    CentOS 的tomcat安装目录:/usr/local/tomcat vi MyTomcatUitl.sh          创建文件chmod u+x MyTomcatUtil.sh   赋执行 ...

  6. Shell script for logging cpu and memory usage of a Linux process

    Shell script for logging cpu and memory usage of a Linux process http://www.unix.com/shell-programmi ...

  7. shell script入门

    从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操 ...

  8. shell script 的追踪与 debug

    shell script 的追踪与 debug scripts 在运行之前,最怕的就是出现语法错误的问题了!那么我们如何 debug 呢?有没有办法不需要透过直接运行该 scripts 就可以来判断是 ...

  9. 第十三章、学习 Shell Scripts 简单的 shell script 练习

    简单的 shell script 练习 简单范例 对谈式脚本:变量内容由使用者决定 [root@www scripts]# vi sh02.sh #!/bin/bash # Program: # Us ...

随机推荐

  1. 用Gen8服务器来学习虚拟化ESXI

    虚拟化和云计算是这几年的热门技术,VMware公司是虚拟化的领头羊,小坦克我有幸在VMware从事这方面的测试工作. 本系列将会讲述我学习虚拟化的一些经历. 将会覆盖一些虚拟化产品: 比如:VMwar ...

  2. jedis,spring-redis-data 整合使用,版本问题异常以及解决。

    最近整合使用redis spring-redis 出现了一下问题 spring:3.2.4.RELEASE jedis: jedis  2.4.2 spring-data-redis: 1.5.2.R ...

  3. 安卓使用pull解析器解析XML文件

    学习一下: public class MainActivity extends Activity { List<City> cityList; @Override protected vo ...

  4. DateFomrat Mask in ASPxTextbox increases width ,How to disable display of dxeErrorCell on ASPxTextBox

    让ASPxTextbox显示yyyy-MM-dd, 需设置 <MaskSettings Mask="yyyy-MM-dd" />.但是会导致ASPxTextbox宽度变 ...

  5. minicom的安装与配置

    分类: LINUX     如果项目中使用的bootloader为 u-boot,那么在用minicom向目标板传送kernel时 会发生一些错误.故若您使用的是u-boot,建议您使用kermit, ...

  6. 【matlab】将matlab中数据输出保存为txt或dat格式

    将matlab中数据输出保存为txt或dat格式 总结网上各大论坛,主要有三种方法. 第一种方法:save(最简单基本的) 具体的命令是:用save *.txt -ascii x x为变量 *.txt ...

  7. Hinet 日本数据处理流程

    ---恢复内容开始--- 推荐网站: http://ju.outofmemory.cn/entry/138571 ridnet.py 将Hinet 的cnt 数据提取为sac数据,参考网站 http: ...

  8. 探索javascript----获得节点计算后样式

    节点计算后样式是一个属性与属性值的值对对象: IE:    node.currentStyle; 非IE: window.getComputedStyle(node,null); 兼容方式: func ...

  9. python分割字符串split,filter函数用法

    现有字符串,需要取出用空格分隔的第一段,操作如下 >>> product_model = ‘WS-C2960G-24TC-L – Fixed Module 0′>>> ...

  10. SVG DOM常用属性和方法介绍

    将以Adobe SVG Viewer提供的属性和方法为准,因为不同解析器对JavaScript以及相关的属性和方法支持的程度不同,有些方法和属性是某个解析器所特有的.SVG支持DOM2标准. 12.2 ...