• 【linux】shell:简单的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

     

    附:

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

    1. #!/bin/bash
    2. #Program:
    3. # User inputs his first name and last name.Program shows his full name.
    4. #History:
    5. #2022-05-15 frist release
    6. # PATH=
    7. # export PATH
    8. read -p "please input your first name:" firstname
    9. read -p "please input your last name:" lastname
    10. echo -e "\nyour name is:${firstname},${lastname}"
    11. exit 0
    12. ~
    13. ~
    14. ~

    1. #program:
    2. # Program creates three files,which named by user input and date command
    3. #History:
    4. #2022-05-16,first release
    5. #from bird linux
    6. #1.to make user input file name,and get varible fileuser
    7. echo -e "i will use 'touch' command to create 3 files"#to show message
    8. read -p "please input your filename": fileuser
    9. #2.to
    10. filename=${fileuser:-"filename"}
    11. #3.date command for filename needed
    12. date1=$(date --date='2 days ago' +%Y%m%d) #date of two day ago
    13. date2=$(date --date='1 day ago' +%Y%m%d) #date of one day ago
    14. date3=$(date +%Y%m%d) #date of today
    15. #4.config file
    16. file1=${filename}${date1}
    17. file2=${filename}${date2}
    18. file3=${filename}${date3}
    19. #5.touch file
    20. touch "${file1}"
    21. touch "${file2}"
    22. touch "${file3}"

    1. #!bin/bash
    2. #program:
    3. # user inputs 2 integer numbers;program will cross these two numbers
    4. #History:
    5. #2022-05-16 from bird linux,first release
    6. echo -e "you should input 2 numbers,i will mult them! \n"
    7. read -p "first number: " first
    8. read -p "second number:" second
    9. total=$((${first}*${second}))
    10. echo -e "\nthe result of ${first}*${second} is==>${total}"
    11. ~
    12. ~
    13. ~
    14. ~
    15. ~
    1. #!/bin/bash
    2. #program:
    3. # user input a scale number to calculate pi number.
    4. #History:
    5. #2022-05-16, bird liunx, first release
    6. echo -e "this program will calculate pi value\n"
    7. echo -e "you should input a float number to calculate pi value\n"
    8. read -p "the scale number (10-1000)?" checking
    9. num=${checking:-"10"} #judge if not input a number
    10. echo -e "starting calcuate pi value,be patient"
    11. time echo "scale=${num};4*a(1)" | bc -lq

  • 相关阅读:
    KT148A电子语音芯片ic方案适用的场景以及常见产品类型
    错字修改 | 布署1个中文文文本拼蟹纠错模型
    Xshell+Xftp通过代理的方式访问局域网内网服务器
    ”linux学习之路” (感觉写的很好,更像是网络编程学习路线图)
    【计算机网络实验】防火墙访问控制列表实验
    Linux编辑器 VI VIM
    RuoYi-Vue Spring Security 配置介绍
    [Spring] SpringMVC 简介(二)
    短期经济波动:均衡国民收入决定理论(三)
    YOLO系列 --- YOLOV7算法(二):YOLO V7算法detect.py代码解析
  • 原文地址:https://blog.csdn.net/m0_52043808/article/details/124787975