标签:shell 脚本
写一个脚本/root/bin/yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息
#!/bin/bash read -p"put your answer(yes or no):" Answer case $Answer in y|Y|[yY][eE][sS]) //判断用户输入yes时的一切可能性 echo "you agree" ;; n|N|[nN][oO]) //判断用户输入no时的一切可能性 echo "you disagree" ;; *) echo "please input the right answer" esac
2.写一个脚本/root/bin/filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)
#!/bin/bash read -p"input the path:" Path if [ ! -e $Path ];then //[]中-e判断是否存在 echo "No such file" exit fi if [ -f $Path ];then echo "$Path is Common file" elif [ -d $Path ];then echo "$Path is Directory" elif [ -l $Path ];then echo "$Path is Symbol file" else echo "Unkown" fi
3.用*打印一个正三角形
#!/bin/bash #version 1.0 read -p "please input the linenumber:" Linenumber for Lineid in `seq 1 $Linenumber` do for A in `seq 1 $[$Linenumber-$Lineid]` //先打空白,然后再打* do echo -n " " done for B in `seq 1 $[2*$Lineid-1]` do echo -n "*" done echo done
执行效果如下:
4.写一个脚本,提示用户输入一个用户名,先判断用户是否存在,如果不存在,则先创建用户,然后再设定密码同用户名
#!/bin/bash # # read -p "input an username:" Username grep "^$Username\>" /etc/passwd &>/dev/null if [ $? -eq 0 ];then echo -e "the \033[31m$Username\033[0m is exist" //echo -e \033[31m***\033[0m为将***着色 else useradd $Username &>/dev/null echo -e "add \033[32m$Username\033[0m successful" echo $Username | passwd --stdin $Username &>/dev/null id $Username fi
5.写一个脚本,提示用户输入正整数N的值,计算1+2+3...+n的总和
#!/bin/bash # # Sum=0 read -p "Input the int n: " I for N in `seq 1 $I`;do //N取的取值从1一直到用户输入数值I if [ $N -le $I ];then let Sum+=$N fi done echo "The sum is $Sum"
6.写一个脚本,计算用户输入几个参数的和
#!/bin/bash # # Sum=0 read -p "Input the int n: " I for N in $I;do //N从用户输入的值中取值,使用方法类似于$@ let Sum+=$N done echo -n "All the parameter sum is:" echo -n "`echo $I | tr " " "+"`" //将用户输入的参数空白换成+ echo -e "=\033[5;31m$Sum\033[0m"
7.写一个脚本,提示用户输入几个正整数,然后计算其总乘积,最后例出用户输入的参数,形如:1*3*10=30
#!/bin/bash # read -p "input some postive integer: " Integer Sum=1 for K in $Integer;do if [ $K -le 0 ];then echo "you are wrong~" exit fi done for I in $Integer;do Sum=$(($Sum*$I)) done echo -n "All the parameter sum is:" echo -n "`echo $Integer | tr " " "*"`" echo -e "=\033[5;31m$Sum\033[0m"
8.找出100以内所有能被3整除的数,每行显示8个数,然后换行显示
#!/bin/bash # # A=0 for I in {1..100};do while [ $(($I%3)) -eq 0 ];do echo -ne "$I\t" let I++ let A++ //计算满足被3整除的个数 if [ $(($A%8)) -eq 0 ];then //被3整除的个数为8的倍数后换行 echo fi done done echo
9.使用while循环编写脚本,使其完成下面的功能
1.提示用户输入两个整数:firstNum和secondNum(fistNum的值一定要小于secondNum)
2.输出所有介于firstNum和secondNum之间的奇数
3.输出所有介于firstNum和secondNum之间的偶数的和。
#!/bin/bash # # read -p "Input 2 numbers,the first must less equal the second one:" First Second while [ $First -gt $Second ];do echo "The first one must less equal the second one,please input again :)" read -p "Input 2 numbers,the first must less equal the second one:" First Second done A=$[$First+1] B=$[$Second-1] //将两者的范围缩小,以满足题目中的介于之间要求 Sum=0 echo -n "The odd in the first number and second number is: " for I in `seq $A $B`;do if [ $[$I%2] -eq 1 -o $[$I%2] -eq -1 ];then //判断I是否为奇数 echo -en "\033[5;32m$I\033[0m " //正确则输出I值 else let Sum+=$I //I值为偶数时则进行累加 fi done echo echo -e "The sum of the even in the first number and second one is:\033[5;31m$Sum\033[0m"
10.只使用while,写一个脚本,提示用户输入一个正整数(如果不是正整数要求用户重新输入),而后输出小于等于该数的所有质数
#!/bin/bash # # read -p "Input the number: " Prime while [[ ! "$Prime" =~ ^[[:digit:]]*$ || $Prime -le 0 ]];do //判断用户输入为正整数 read -p "You need put a positive integer: " Prime done I=1 echo -n "The primes in $Prime is: " while [ $I -le $Prime ];do K=1 N=0 J=2 while [ $J -lt $(($I-1)) ];do //J取值从2开始,取1开始的话后面无法进入循环 while [ $(($I%$J)) -eq 0 -a $N -eq 0 ];do //判断I对J取模的结果是否为0 K=0 //记录一个返回值,不能直接输出I值,避免多次判断多次输出 N=1 //如果满足条件进入循环,设定一个跳出值 done let J++ done M=0 //同上N值的设定,为跳出循环 while [ $K -eq 1 -a $M -eq 0 ];do //判断返回值是否为初始值 echo -n "$I " //因为不为质数则进入上述循环,会将返回值变为K=0 M=1 done let I++ done echo
注:此处用for和if结合使用时脚本相对会简单,有兴趣的同学可以自行编写
11.计算距离自己生日还有多少天
#!/bin/bash # # read -p "Input your the month of yourbirthday,the format such as 05 :" Birthmonth while [[ ! "$Birthmonth" =~ [0-9][0-9] || $Birthmonth -lt 1 || $Birthmonth -gt 12 ]];do //判断用户是否输入两位数字,个位数需用零补全 read -p "Input two digits,the value must between in 01 and 12,such as 04 :" Birthmonth done read -p "Input your the day of your birthday,the format such as 25 : " Birthday until [[ $Birthday =~ [0-9][0-9] && $Birthday -gt 0 && $Birthday -le 31 ]];do read -p "Input two digits,and the value must in 01 and 31,such as 25 :" Birthday done Year=`date +%Y` //定义变量,简化后续引用 J=`date +%s` if [ $Birthmonth$Birthday -eq `date +%m%d` ];then //if判断如果生日与现在时间相等 echo -e "\033[5;31mToday is your birthday,happy birthday to you.\033[0m" elif [ $Birthmonth$Birthday -gt `date +%m%d` ];then //如果生日还未到 I=`date -d $Year$Birthmonth$Birthday +%s` echo -e "\033[5;32m$(((($I-$J))/60/60/24+1))\033[0m days later will be your birthday" else //else生日已过的情况 if [ $Birthmonth$Birthday -eq 0229 ];then //如果生日是2/29号 case $((`date +%Y`%4)) in //判断今年是否是闰年 0) //如果今年是闰年 I=`date -d $(($Year+4))$Birthmonth$Birthday +%s` echo -e "\033[5;35m$(((($I-$J))/60/60/24+1))\033[0m days later will be your birthday" ;; 1) I=`date -d $(($Year+3))$Birthmonth$Birthday +%s` echo -e "\033[5;35m$(((($I-$J))/60/60/24+1))\033[0m days later will be your birthday" ;; 2) I=`date -d $(($Year+2))$Birthmonth$Birthday +%s` echo -e "\033[5;35m$(((($I-$J))/60/60/24+1))\033[0m days later will be your birthday" ;; 3) I=`date -d $(($Year+1))$Birthmonth$Birthday +%s` echo -e "\033[5;35m$(((($I-$J))/60/60/24+1))\033[0m days later will be your birthday" ;; esac else //如果生日不是2/29号 I=`date -d $(($Year+1))$Birthmonth$Birthday +%s` echo -e "\033[5;35m$(((($I-$J))/60/60/24+1))\033[0m days later will be your birthday" fi fi
标签:shell 脚本
原文地址:http://mghuee.blog.51cto.com/11643000/1792848