linuxshell:简单的shell脚本练习

Posted 暮色_年华

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linuxshell:简单的shell脚本练习相关的知识,希望对你有一定的参考价值。

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

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

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

交互式脚本:

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

vim showname.sh

执行脚本:

利用date建立文件:

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

 数值运算:

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

 数值计算:

bc命令:

 (不能使用/)

 题:通过bc计算pi

 

附:

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

以上是关于linuxshell:简单的shell脚本练习的主要内容,如果未能解决你的问题,请参考以下文章

linux shell脚本学习

linuxShell的简单练习

简单而有效的Linux Shell脚本示例(续一)

linuxshell 脚本执行的过程是怎样的?

linux shell脚本执行命令详解

Linux shell脚本流程控制