zoukankan      html  css  js  c++  java
  • Linux学习之shell script

    一、撰写一个script,让使用者输入:1.first name 2.last name,最后在屏幕上显示:Your full name is:的内容

    1
    2
    3
    4
    #!/bin/bash
    read -p "Please input your firstname:" firstname
    read -p "Please input your lastname:" lastname
    echo -e "Your full name is:$firstname $lastname"

    二、用户输入2个变量,然后将2个变量相乘,最后输出相乘结果

    1
    2
    3
    4
    5
    #!/bin/bash
    read -p "input first number:" firstnu
    read -p "input second number:" secnu
    total=$(($firstnu*$secnu))
    echo -e "the result of $firstnu x $secnu is $total"

    三、使用source执行script,可将变量置于父进程(环境)中

    四、用户输入一个filename,并做如下判断:

    1. filename的档案是否存在,不存在就终止程序

    2. 若存在,则判断是文件还是目录,并输入结果

    3. 判断当前身份用户对该档案/目录所具有的权限,并输出结果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    #!/bin/bash
    echo -e "Please input a filename,this program will check the file's type and permission"
    read -p "Input a filename:" filename
     
    #1.判断使用者是否真有输入字符串
    test -z $filename&&echo "You must input a filename"&& exit 0
     
    #2.判断档案是否存在,不存在则终止程序
    test ! -e $filename&&echo "the filename $filename no exist"&& exit 0
     
    #3.判断文件类型及属性
    test -f $filename&&filetype="regular file"
    test -d $filename&&filetype="directory"
    test -r $filename&&perm="readable"
    test -w $filename&&perm="$perm writable"
    test -x $filename&&perm="$perm executable"
     
    #4.开始输出信息
    echo "The filename: $filename is a $filetype"
    echo "The permisson are: $perm"

    五、使用中括号代替test进行判断

    1
    2
    3
    4
    5
    #!/bin/bash
    read -p "Please choose Y/N:" yn
    "$yn" == "y" -o "$yn" == "Y" ]&&echo -e "OK,continue"&&exit 0
    "$yn" == "n" -o "$yn" == "N" ]&&echo -e "Oh,interrupt"&&exit 0
    echo -e "I don't know what you mean"&&exit 0

    六、使用if..elif..then..fi判断式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #!/bin/bash
    read -p "Please choose Y/N:" yn
    if "$yn" == "Y" ]||[ "$yn" == "y" ];
    then
    echo -e "OK,continue"
     
    elif "$yn" == "N" ]||[ "$yn" == "n" ];
    then
    echo -e "Oh,interrupt"
     
    else
    echo -e "I don't know what you mean"
    fi

     

  • 相关阅读:
    mysql数据库连接报错ERRoR1130(HY000)
    Ladap相关知识精简资料
    Github访问慢,解决方案
    IIS短文件漏洞成因,及修复方案
    Kali linux简单匿名处理方法
    NHibernate实践总结(二) 在mapping文件中设置抓取策略对HQL与Criteria造成不同影响的测试与验证
    NHibernate 3.x新功能实践(二) QueryOver(下)
    NHibernate实践总结(三)HQL的thetastyle join对无关联实体的连接与应用
    NHibernate实践总结(一)
    Scott Hanselman's 2009 NET与Windows终极开发工具列表
  • 原文地址:https://www.cnblogs.com/enginex/p/6802723.html
Copyright © 2011-2022 走看看