zoukankan      html  css  js  c++  java
  • Shell 语句

    一 test 测试:

    测试命令 test [ ] [[ ]] (( ))
    打开man test 逐一介绍每个参数

    浮点计算:echo 'scale=2;1/3'|bc -l
     

    测试操作
    命令执行后会返回到一个系统变量中 $?
    如果$?值为0 表示命令执行成功 否则为失败

    二流程控制: if   while   for

    #!/bin/bash
    read -p 'please input username:' usr
    read -p 'please input passwd:' passwd
    
    if [ $usr = 'alex' -a $passwd = 'alex3714' ];then
       echo 'login successful'
    else
       echo 'username or password is worng'
    fi
    用户测试
    !/bin/bash
    age=57
    while :
    do
    
    read -p 'input oldboy age:' ag
    
    if [ $ag -eq $age ];then
        echo "bingo"
        break
    elif [ $ag -gt $age ];then
        echo "the age is older"
    else
        echo "the age is younger"
    fi
    
      if [ -z $ag ];then
      continue
      fi
    
    done
    猜年龄
    #!/bin/bash
    read -p 'please input your score:' score
    
    if [ $score -ge 90 ];then
      echo 'excellent'
    elif [ $score -ge 70 -a $score -lt 90 ];then
      echo 'good'
    elif [ $score -ge 60 -a $score -lt 70];then
      ehco 'not bad'
    else
      echo 'bad'
    fi
    成绩查询
    #!/bin/bash
    read -p 'input your file:  ' file
    if [ -p $file ];then
       echo "$file is block file"
    elif [ -f $file ];then
       echo "$file is reuler file"
    elif [ -d $file ];then
       echo "$file is directory file"
    else
       echo "$file is unkown"
    fi
    测文件类型
    #!/bin/bash
    for ((i =1;i<=9;i++))
    do
       for ((j=1;j<=i;j++))
       do
         echo -n "$i*$j=$[$i*$j] "
       done
    echo
    done
    九九乘法表
    #!/bin/bash
    usr='mona'
    passwd='123'
    tag=true
    while $tag
    do
      read -p 'please input your name: ' name
      read -p 'please input your password: ' pd
      if [ $name = $usr ] && [ $pd = $passwd ];then
      echo 'login successful'
        while $tag
        do
           read -p 'input your indirction: ' cmd
           if [ $cmd = 'quit' ];then
            tag=false
           else
               $cmd
            fi
        done
      fi
    done
    登录用户操作
  • 相关阅读:
    记录:2019-06-15
    安卓APP环境搭建
    delphi 各版本的特性
    php.ini文件下载
    mysql数据库目录my.ini的内容
    Windows2008 R2 X64 PHP环境搭建步骤
    窗口关闭时弹出内存不能为read
    Delphi编译选项
    Android中EditText无法再次获得焦点
    Android设置分隔线
  • 原文地址:https://www.cnblogs.com/mona524/p/6945425.html
Copyright © 2011-2022 走看看