首页 > 代码库 > Shell 语句

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
登录用户操作

 

Shell 语句