linux sheel script demo
demo1 (输入/输出)
1.1. target :
输入姓、名, 输出姓名
1.2. create directory
mkdir ~/bin
1.3. create & edit sheel script
vim fullname.sh
note: more comment is useful
#!/bin/bash
#Program
# User inputs his first name and last name . Program shows his full name.
#History :
#// logan First lease
PATH=/bin:$PATH #将当前目录加入系统环境
export PATH
read -p "Please input your firstname:" firstname
read -p "Please input your lastname:" lastname
echo -e "\nYour full name is :" ${firstname} ${lastname}
1.4. run
[rocky@localhost bin]$ sh fullname.sh
demo2 (计算PI)
2.1 target
输入小数点后位数 输出PI
2.2 create & edit
vim cal_pi.sh
#!/bin/bash
#Program:
# Usre input a scale number to calculate pi number.
#History:
#// logan First lease
PATH=/bin:PATH
export PATH
echo -e "This program will calculate pi value. \n"
echo -e "You should input a float number to calculate pi value. \n"
read -p "This scale number (10-10000) ? " checking
num=${checking:-""}
echo -e "Starting calculate pi value. Be patient."
time echo "scale=${num}; 4*a(1)"|bc -lq
2.3 run
输入 5
输出 3.14156
demo3 ($# $@ $* 的使用)
3.1 target
输入参数, 显示相关结果
3.2 create & edit sheel
vim how_paras.sh
#!/bin/bash
#Program:
# Program shows the script name, parameters
#History:
#// logan First release
PATH=/bin:$PATH
export PATH echo "The script name is ==> ${0}"
echo "Total parameter number is ==> $#"
[ "$#" -lt ] && echo "The number of parameter is less than 2. Stop here." && eexit
echo "Your whole parameter is ==> '$@'"
echo "The 1st parameter ==> ${1}"
echo "The 2nd parameter ==> ${2}"
3.3 run
[logan@localhost bin]$ sh how_paras.sh zhang li wang
The script name is ==> how_paras.sh
Total parameter number is ==> 3
Your whole parameter is ==> 'zhang li wang'
The 1st parameter ==> zhang
The 2nd parameter ==> li
demo4 (shift 使用)
4.1 vim how_shift.sh
.....#省略注释 PATH......
echo "The script name is ==> ${0}"
echo "Total parameter number is ==> $#"
[ "$#" -lt ] && echo "The number of parameter is less than 6. Stop here." && eexit
echo "Your whole parameter is ==> '$@'"
shift
echo "------------------shift one--------"
echo "Total parameter number is ==>$#"
echo "The whole parameter is $@"
shift
echo "------------------shift three------"
echo "Total parameter number is ==>$#"
echo "the whole parameter is ==> $@"
4.2 run
[logan@localhost bin]$ sh how_shift.sh one two three four five six
The script name is ==> how_paras.sh
Total parameter number is ==> 6
Your whole parameter is ==> 'one two three four five six'
------------------shift one--------
Total parameter number is ==>5
The whole parameter is two three four five six
------------------shift three------
Total parameter number is ==>2
the whole parameter is ==> five six
[logan@localhost bin]$ vim how_paras.sh
demo5 (if 条件判断式)
5.1 vim if_fi.sh
#!/bin/bash
#Program:
# This program shows the user's choice
#History:
# logan first release
PATH=/bin:$PATH
export PATH 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
note: 中括号内 变量及字符两边需要空格符,否则报错 '找不到命令'
5.2 run
sh if_fi.sh
demo6 (日期计算)
6.1 vim cal_retired.sh
#!/bin/bash
#Program:
# You input your demobilization date, I calculate how many days before you demobilize.
#History:
#// logan first release
PATH=/bin:$PATH
export PATH #. tell user the function of the shell script, and the format of the date
echo "This program will try to calculate:"
echo "How many days before your demobilization date..."
read -p "Please input your demobilization date (YYYYMMDD ex>20131230): " date2 #. test the comment you just input with the Pattern
date_d=$(echo ${date2} |grep '[0-9]\{8\}') # check length whether of the input num
if [ "${date_d}" == "" ];then
echo "You input the wrong date format...."
exit
fi #. begin to calculate the date
declare -i date_dem=$(date --date="${date2}" +%s) #seconds count of the input date
declare -i date_now=$(date +%s) #seconds count date now
declare -i date_total_s=$((${date_dem}-${date_now})) #differ from the two date
declare -i date_d=$((${date_total_s}///)) #transfer to days
if [ "${date_total_s}" -lt "" ];then
echo "You had been demobization before: " $((-*${date_d})) " days ago"
else
declare -i date_h=$(($((${date_total_s}-${date_d}***))//))
echo "You will demobilize after ${date_d} days and ${date_h} hours."
fi
6.2 run
sh cal_retired.sh
demo7 (case..esac)
7.1 vim hello-3.sh
#!/bin/bash
#Program:
# Show "Hello" from $.... by using case .... esac
#History:
#// logan First lease
PATH=/bin:$PATH
export PATH case ${} in
"hello")
echo "Hello, how are you?"
;;
"")
echo "You MUST input parameters, ex> {${0} someword}"
;;
*)
echo "Usage ${0} {hello}"
;; esac
7.2 run
sh hello-3.sh
demo8 (function使用)
vim demo.sh
#!/bin/bash
#Program:
# Show "one two three" from $.... by using case .... esac
#History:
#// logan First lease
PATH=/bin:$PATH
export PATH function printit(){
echo -n "Your choice is " #-n means not enter to next line
} case ${} in
"one")
printit; echo ${} | tr 'a-z' 'A-Z' #transfer to Upper
;;
"two")
printit; echo ${} | tr 'a-z' 'A-Z' #transfer to Upper
;;
"three")
printit; echo ${} | tr 'a-z' 'A-Z' #transfer to Upper
;;
*)
echo "Usage ${0} {one|two|three}"
;; esac
8.2 run
sh demo.sh one
demo9
9.1 vim while_do_done.sh
#!/bin/bash
#Program:
# Repeat question until user input correct answer.
#History:
#// logan first release
PATH=/bin:$PATH
export PATH 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."
9.2 run
sh while_do_done.sh
demo10
10.1 vim add_1_to_100.sh
#!/bin/bash
#Program:
# calculate the sum of num ,...
#History:
#// logan first release
PATH=/bin:$PATH
export PATH s=
i=
while [ "${i}" != "" ]
do
i=$(($i+))
s=$(($s+i))
done
echo "The result of '1+2+3...+100' is ==> $s"
10.2 run
sh add_1_to_100.sh
10.3 vim add_1_to_num.sh
#!/bin/bash
#Program:
# calculate the sum of num ,...
#History:
#// logan first release
PATH=/bin:$PATH
export PATH s=
i=
j=
while [ "${j}" -le "" ]
do
read -p "Please input the num: " j
done
while [ "${i}" != "${j}" ]
do
i=$(($i+))
s=$(($s+i))
done
echo "The result of '1+...+ ${j}' is ==> $s"
demo11
11.1 vim for_test.sh
#!/bin/bash
PATH=/bin:$PATH
export PATH for animal in dog cat elephant
do
echo "There are ${animal}s..."
done
11.2 sh for_test.sh
demo12
12.1 target
ping 局域网内 192.168.1.1~100的看通不通, seq(sequence)指令的使用
12.2 vim pingip.sh
#!/bin/bash
#Program
# Use ping command to check the network's PC state
#History
#2017/04/14 logan first release
PATH=/bin:$PATH
export PATH network="192.168.1"
for sitenu in $(seq 1 100) #seq 为 sequence(连续)的缩写
do
ping -c 1 -w 1 ${network}.${sitenu} &> /dev/null && result=0 || result=1
if [ "${result}" == 0 ];then
echo "Server ${network}.${sitenu} is UP."
else
echo "Server ${network}.${sitenu} is DOWN."
fi
done
12.3 run
sh pingip.sh
demo13
13.1 target
判断式(test)的使用
13.2 vim dir_perm.sh
#!/bin/bash
#Program
# User input dir name, I find the permission of files.
#History:
#2017/04/14 logan first release
PATH=/bin:$PATH
export PATH read -p "Please input a directory: " dir
if [ "${dir}" == "" -o ! -d "${dir}" ]; then
echo "The ${dir} is NOT exist in your system."
exit 1
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
13.3 run
sh dir_perm.sh
demo14
14.1 description
for循环的使用
14.2 vim cal_1_nu.sh
#!/bin/bash
#Program
# Try do calculate 1+2+..+ ${your_input}
#History:
#2017/04/14 logan first release
PATH=/bin:$PATH
export PATH read -p "Please input a number, I will count for 1+2..+ your_input:" nu s=0
for (( i=1; i<=${nu}; i=i+1))
do
s=$((${s}+${i}))
done
echo "The result of '1+2+...${nu}' is ==> ${s}"
demo15
15.1 target
随机选择3种菜
15.2 vim what_to_eat3.sh
#!/bin/bash
#Program
# Try do tell you what you may eat.
PATH=/bin:$PATH
export PATH eat[]="zhang san ji"
eat[]="li si ji"
eat[]='wang wu ji'
eat[]="zhang liu ji"
eat[]="qian qi ji"
eat[]="hao 1"
eat[]="hao 2"
eat[]="hao 3"
eat[]="hao 4"
num= count=
while [ "${count}" -lt ]; do
check=$(( ${RANDOM} * ${num} / + ))
flag=
if [ "${count}" -ge ];then
for i in $(seq ${count} )
do
if [ "${selectedNum[$i]}" == $check ];then
flag=
fi
done
fi
if [ ${flag} == ];then
echo "You mat eat ${eat[${check}]}"
count=$((${count} + ))
selectedNum[${count}]=${check} fi
done
demo16 (计算生日)
16.1 vim birthday_howlong.sh
#!/bin/bash
#Program
# how many days your birthday remain
#History
#// logan first release
read -p "Please input your birthday (MMDD, ex>0709): " bir
now=`date +%m%d`
if [ "$bir" == "$now" ];then
echo "Happy birthday to you!!!"
elif [ "$bir" -gt "$now" ];then
year=`date +%Y`
total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))///))
echo "Your birthday will be $total_d later"
else
year=$((`date +%Y`+))
total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))///))
echo "Your birthday will be $total_d later"
fi
demo17 (show accounts)
17.1 vim show_account.sh
#!/bin/bash
accounts=`cat /etc/passwd | cut -d':' -f1`
for account in $accounts
do
declare -i i=$i+
echo "The $i account is \"$account\" "
done
linux sheel script demo的更多相关文章
- linux SPI bus demo hacking
/********************************************************************** * linux SPI bus demo hacking ...
- (原创)鸟哥linux学习script shell相关笔记
在使用鸟哥linux进行script shell学习的过程中碰到一些不太明白的知识点,在这里进行一些记录 1. [root@www scripts]# vi sh03.sh #!/bin/bash # ...
- 一个改动配置文件的linux shell script
不久以前,以前搜到一篇博客是读取配置文件的,http://www.cnblogs.com/bo083/archive/2012/11/19/2777076.html,用到如今,感觉十分方便.感谢作者. ...
- Linux bash script regex auto replace
Linux bash script regex auto replace 自动替换 /assets/css/0.styles.96df394b.css => ./assets/css/0.sty ...
- Linux Bash Script conditions
Linux Bash Script conditions shell 编程之条件判断 条件判断式语句.单分支 if 语句.双分支 if 语句.多分支 if 语句.case 语句 refs http:/ ...
- Linux Bash Script loop
Linux Bash Script loop shell 编程之流程控制 for 循环.while 循环和 until 循环 for var in item1 item2 ... itemN do c ...
- Linux shell script All In One
Linux shell script All In One refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
- Linux下script命令录制、回放和共享终端操作script -t 2> timing.log -a output.session # 开始录制
Linux下script命令录制.回放和共享终端操作 [日期:2018-09-04] 来源:cnblogs.com/f-ck-need-u 作者:骏马金龙 [字体:大 中 小] 另一篇终端会话共 ...
- 【学习笔记】linux bash script
1. sed sed 是一种流编辑器,它是文本处理中非常常用的工具,能够完美的配合正则表达式使用,功能非常强大. mkdir playground touch test.txt echo " ...
随机推荐
- Tomcat安装与使用
主要讲解Tomcat的 安装与使用,讲解ubuntu版本和windows. 下载与安装: 1)到apache官网.www.apache.org http://jakarta.apache.org(产品 ...
- MD5算法分析
1. MD5是什么? MD5即Message-Digest Algorithm 5(消息摘要算法第五版)的简称,是当前计算机领域用于确保信息传输完整一致而广泛使用的散列算法之一(又译哈希算法.摘要算法 ...
- Java实现文件重命名
最近在做一个Android上面的一个文件管理器的apk,有文件名重命名和剪切的功能. 一般的思路如下: 重命名:先新建一个文件,复制原先的文件,读写文件,最后删除原先文件 剪切:先复制原先的文件,删除 ...
- [转]IOS应用程序多语言本地化解决方案
最近要对一款游戏进行多语言本地化,在网上找了一些方案,加上自己的一点点想法整理出一套方案和大家分享! 多语言在应用程序中一般有两种做法:一.程序中提供给用户自己选择的机会:二.根据当前用户当前移动设备 ...
- Java的定时调度
一般在web开发中定时调度比较有用,因为要维护一个容器不关闭才可以一直定时操作下去. 定时调度:每当一段时间之后,程序就会自动执行,就称为定时调度.如果要使用定时调动,则必须要保证程序要始终运行着,也 ...
- 分布式文件系统之FastDFS
环境引入: 在一个大型的教育官网,会拥有大量优质的视频教程,并且免费提供给用户去下载,文件太多如果高效存储?用户访问量大如何保证下载速度?分布式文件系统是解决这些问题的有效方法之一 一.什么是文件系统 ...
- 矩阵快速幂--51nod-1242斐波那契数列的第N项
斐波那契额数列的第N项 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, ...
- 批量生成python自动化测试脚本
先前有家供应商与我们合作开发自动化工程,采用的py unittest作为脚本运行框架.我发现他们出的脚本都是挨个手写的,格式上也是参差不齐.所以有了根据用例表批量生成脚本的一段小代码 对一个测试脚本必 ...
- my.等级限制
1.20190405 “春之恋曲 4月5日双平台新服开服公告”,20190426 上去新建了一个号 发现等级限制是 66级(2天后开启新等级) 20190412 “胭脂雪 4月12日双平台新服开服 ...
- spark第十一篇:spark-submit命令支持选项
[root@linux-node3 bin]# ./spark-submit --help Usage: spark-submit [options] <app jar | python fil ...