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

发布时间:2022-06-20 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【linux】shell:简单的shell脚本练习脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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

交互式脚本:

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

vim showname.sh

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

执行脚本:

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

利用date建立文件:

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

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

 数值运算

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

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

 数值计算:

bc命令:

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

 (不能使用/)

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

 题:通过bc计算pi

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

 

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

附:

#!/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

#!/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
~                                                                               
~                                                                               
~                     

#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}"

#!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}"
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                              
#!/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 valuen"
echo -e "you should input a float number to calculate pi valuen"
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

脚本宝典总结

以上是脚本宝典为你收集整理的【linux】shell:简单的shell脚本练习全部内容,希望文章能够帮你解决【linux】shell:简单的shell脚本练习所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。