linux shell 脚本

Shell notes

鸟哥的私房菜 第13章

 

l  编写一个shell脚本,需要用”#!/bin/bash”开头:

#!/bin/bash

# Program:

#      “这里输入此脚本的功能描述”

# History:

# “输入日期”  “输入作者” “输入版本”

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

export PATH

 

read –p “Please input your first name: “ firstname  # 提示用户输入

read –p “Please input your last name: “ lastname  # 提示用户输入

echo –e “\nYour full name is: $firstname $lastname” #结果由屏幕输出

 

l  编写一个shell脚本,创建3个以你输入的字符串开头,并以日期结尾的文件。

#!/bin/bash

# Program:

#      “创建3个以你输入的字符串开头,并以日期结尾的文件。”

# History:

# “20171226” “Hongbo” “first release”

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

export PATH

 

# 让用户输入文件名,并取得fileusr这个变量

echo –e “I will use ‘touch’ command to create 3 files.”

read –p “Please input your filename: “ fileuser

 

# 为了避免用户随意按[Enter],利用变量功能分析文件名是否有设置

filename=${fileuser:-“filename”}

 

# 开始利用date命令来取得所需要的文件名了

date1=$(date --date=’2 days ago’ +%Y%m%d)

date2=$(date --date=’1 days ago’ +%Y%m%d)

date3=$(date +%Y%m%d)

file1=${filename}${date1}

file1=${filename}${date2}

file1=${filename}${date3}

 

# 创建文件名

touch “$file1”

touch “$file2”

touch “$file2”

 

 

 

 

 

l  编写一个脚本,进行简单的加减乘除

#!/bin/bash

# Program:

#      “创建3个以你输入的字符串开头,并以日期结尾的文件。”

# History:

# “20171226” “Hongbo” “first release”

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

export PATH

 

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 “\nThe result of $firstnu x $secnu is ==> $total”

 

l  Script 的执行方式区别 (3种执行脚本的方式:sh ;source;直接)

sh sh02.sh 和 ./sh02.sh 都是在让sh02.sh在一个新开的子进程bash中执行脚本,脚本执行过程中产生的变量,在脚本执行结束后,就不存在了。

而source sh02.sh在脚本执行过程中产生的变量,会在执行结束后依然存在于bash中

 

l  条件判断

if [条件]; then

       执行语句

fi

posted @ 2017-12-26 14:27  Apollo_zhanghongbo  阅读(413)  评论(0编辑  收藏  举报