交互式脚本:变量内容由用户决定

read -p 变量:读取值给变量

echo -e ${变量}:显示变量的值

交互式脚本:

题:编写一个脚本,可以让用户输入firstname和lastname,最后在屏幕上显示your full name is:

vim showname.sh

【linux】shell:简单的shell脚本练习_bc

执行脚本:

【linux】shell:简单的shell脚本练习_bc_02

利用date建立文件:

题:想要建立三个空文件,文件名由用户输入决定,假设用户收入filename,而今天的日期是2022-05-16,则建立文件filename_20220516,fileame_20220515,filename_20220514。

【linux】shell:简单的shell脚本练习_debian_03

 数值运算:

题:输入两个变量,将两个变量的内容相乘

【linux】shell:简单的shell脚本练习_bc_04

 数值计算:

bc命令:

【linux】shell:简单的shell脚本练习_bc_05

 (不能使用/)

【linux】shell:简单的shell脚本练习_bc_06

 题:通过bc计算pi

【linux】shell:简单的shell脚本练习_debian_07

 

【linux】shell:简单的shell脚本练习_bc_08

附:

#!/bin/bash
#Program:
#     This program shows'hello,world'on your screen
#History:
#2022-05-15 first release
PAHT=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
# export PATH
echo -e "hello,world \a \n"
exit 0
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
#!/bin/bash
#Program:
#  User inputs his first name and last name.Program shows his full name.
#History:
#2022-05-15 frist release

# PATH=
# export PATH
read -p "please input your first name:" firstname
read -p "please input your last  name:" lastname
echo -e  "\nyour name is:${firstname},${lastname}"
exit 0
~                                                                               
~                                                                               
~
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
#program:
#    Program creates three files,which named by user input and date command
#History:
#2022-05-16,first release
#from bird linux

#1.to make user input file name,and get varible fileuser
echo -e "i will use 'touch' command to create 3 files"#to show message
read -p "please input your filename": fileuser
#2.to
filename=${fileuser:-"filename"}
#3.date command for filename needed
date1=$(date --date='2 days ago' +%Y%m%d)  #date of two day ago
date2=$(date --date='1 day ago' +%Y%m%d)   #date of one day ago
date3=$(date +%Y%m%d)  #date of today
#4.config file
file1=${filename}${date1}
file2=${filename}${date2}
file3=${filename}${date3}
#5.touch file
touch "${file1}"
touch "${file2}"
touch "${file3}"
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
#!bin/bash
#program:
#    user inputs 2 integer numbers;program will cross these two numbers
#History:
#2022-05-16 from bird linux,first release
echo -e "you should input 2 numbers,i will mult them! \n"
read -p "first number: " first
read -p "second number:" second
total=$((${first}*${second}))
echo -e "\nthe result of ${first}*${second} is==>${total}"
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
#!/bin/bash
#program:
#    user input a scale number to calculate pi number.
#History:
#2022-05-16, bird liunx, first release
echo -e "this program will calculate pi value\n"
echo -e "you should input a float number to calculate pi value\n"
read -p "the scale number (10-1000)?" checking
num=${checking:-"10"} #judge if not input a number
echo -e "starting calcuate pi value,be patient"
time echo "scale=${num};4*a(1)" | bc -lq
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.