Linux基础

对谈式脚本:变数内容由使用者决定
#!/bin/bash
# Program:
#   User inputs his first name and last name.  Program shows his full name.
# History:
# 2005/08/23    VBird   First release
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" # 结果由荧幕输出


随日期变化:利用 date 进行档案的建立
鸟哥的 Linux 私房菜 -- 基础学习篇

#!/bin/bash
# Program:
#   Program creates three files, which named by user's input 
#   and date command.
# History:
# 2005/08/23    VBird   First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# 1. 让使用者输入档案名称,并取得 fileuser 这个变数;

echo -e "I will use 'touch' command to create 3 files." # 纯粹显示资讯

read -p "Please input your filename: " fileuser # 提示使用者输入

# 2. 为了避免使用者随意按 Enter ,利用变数功能
分析档名是否有设定?

filename=${fileuser:-"filename"} # 开始判断有否设定档名

# 3. 开始利用 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} # 底下三行在设定档名

file2=${filename}${date2}
file3=${filename}${date3}

# 4. 将档名建立吧!

touch "$file1" # 底下三行在建立档案

touch "$file2"
touch "$file3"


数值运算:简单的加减乘除
#!/bin/bash
# Program:
#   User inputs 2 integer numbers; program will cross these two numbers.
# History:
# 2005/08/23    VBird   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"


你可能感兴趣的:(Linux基础)