编写一个linuxshell脚本,运行它提示输入你的姓名,当你输入完毕之后程序再次将你输入的结果显示在屏幕上。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编写一个linuxshell脚本,运行它提示输入你的姓名,当你输入完毕之后程序再次将你输入的结果显示在屏幕上。相关的知识,希望对你有一定的参考价值。

#!/bin/bash
read -p "请输入你的名字:" name
echo $name
参考技术A #!/bin/bash 这里写你的shell文件的位置,如果是linux的bash就应该是这个
read -p "Please input your name: " name 读入名字
echo -e "\nYour full name is: $name 输出名字

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:简单的shell脚本练习

linux中在shell中怎么创建一个数据库

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

LinuxShell脚本从入门到比入门强一点点的坎坷之路