9月15日做业

一、写一个脚本:若是某路径不存在,则将其建立为目录;不然显示其存在,并显示内容类型;bash

    #!/bin/bash
  
    path="/tmp/a/b/fang"
    if [ -e $path ]; then
    	echo "$path exists!"
    	file $path
    else
    	mkdir -p $path
    fi


二、写一个脚本,完成以下功能;判断给定的两个数值,孰大孰小;给定数值的方法:脚本参数,命令交互;ide

    #!/bin/bash
    
    read -p "Please input two number: " -t10 num1 num2
    
    if [ -z "$num1" ]&&[ -z "$num2" ]; then
    	echo "Please give two numbers."
    	exit 1
    fi
    
    if [ $num1 -ge $num2 ]; then
    	echo "MAX:$num1 MIN:$num2"
    else
    	echo "MAX:$num2 MIN:$num1"
    fi


三、求100之内全部奇数之和函数

方法1:ui

    #!/bin/bash
    
    declare -i sum=0

    for i in $(seq 0 2 100); do
        sum=$(($sum+$i))
    done

    echo "Even sum: $sum."

方法2:spa

    #!/bin/bash

    declare -i sum=0

    for i in {1..100}; do
	if [ $[$i%2] -eq 0 ]; then
		sum=$[$sum+$i]
	fi
    done

    echo "Even sum: $sum."

方法3:字符串

    #!/bin/bash
    
    declare -i sum=0
    add(){
    	for i in $(seq 0 2 100); do
    		sum=$[$sum+i]
    	done
    }
    add
    echo "The sum is $sum."


四、写一个脚本实现以下功能:input

            (1) 传递两个文本文件路径给脚本;string

            (2) 显示两个文件中空白行数较多的文件及其空白行的个数;it

            (3) 显示两个文件中总行数较多的文件及其总行数;io

    #!/bin/bash
    
    text1=sedawkprocess0915.txt
    text2=sedawkprocess0917.txt
    
    tj1=`grep '^$' sedawkprocess0915.txt | wc -l`
    tj2=`grep '^$' sedawkprocess0917.txt | wc -l`
    
    total1=`cat sedawkprocess0915.txt | wc -l`
    total2=`cat sedawkprocess0917.txt | wc -l`
    
    if [ $tj1 -gt $tj2 ]; then
    	echo "$text1 total blank lines are $tj1"
    else
    	echo "$text2 total blank lines are $tj2"
    fi
    	
    if [ $total1 -gt $total2 ]; then
    	echo "$text1 total lines are $total1"
    else
    	echo "$text2 total lines are $total2"
    fi


五、写一个脚本

            (1) 提示用户输入一个字符串;

            (2) 判断:

            若是输入的是quit,则退出脚本;

            不然,则显示其输入的字符串内容;

    #!/bin/bash
    
    read -p  "Please input a string: " -t10 string
    
    if [ $string == "quit" ]; then
    	exit 0
    else
    	echo "This string is $string."
    	
    fi


六、写一个脚本,打印2^n表;n等于一个用户输入的值;

    #!/bin/bash

    read -p "Please input a number: " -t10 number
    i=0
    while [ $i -lt $number ];do
    	i=$[$i+1]
    	echo -n -e "2^$i=$[2**$i]"
    	echo
    done


七、写一个脚本,写这么几个函数:函数一、实现给定的两个数值的之和;函数二、取给定两个数值的最大公约数;函数三、取给定两个数值的最小公倍数;关于函数的选定、两个数值的大小都将经过交互式输入来提供。

    #!/bin/bash

    if [[ $# -le 2 ]];then
      echo "plz input two number and use space to isolation them!"
    fi
    
    function add() {
    	echo "The two num sum:$1+$2=`expr $1 + $2`"
    }
    add $1 $2
    
    declare -i big
    declare -i small
    if [ $1 -gt $2 ];then
        big=$1
        small=$2
    else
        big=$2
        small=$1
    fi
    
    gcdlcm(){
    	i=1      #定义一个循环变量
    	GCD=1    #定义最大公约数
    	LCM=1    #定义最小公倍数
    	btmp=1   #定义用户输入的较大的一个变量除以循环变量的值
    	stmp=1   #定义用户输入的较小的一个变量除以循环变量的值
    	  while [ $i -le $small ];do     #定义循环条件,循环变量小于或等于用户输入的较小的变量
    		btmp=`expr $big % $i`      #求值
    		stmp=`expr $small % $i`      #求值
    		  if [ $btmp -eq 0 ];then  #判断值得余数是否为0
    			 if [ $stmp -eq 0 ];then  #同上
    				gcd=$i    #若是值得余数为0,则获取最大公约数的值
    			 fi   #判断结束
    		  fi
    	   i=`expr $i + 1`   #i变量循环,直到i等于用户输入的较小的数为止退出循环。
    	done   #当i=$sma,退出循环
    	lcm=`expr $small / $gcd`   #根据最小公倍数公式求值
    	lcm=`expr $lcm \* $big`  #同上,求最小公倍数公式为:lcm=$sma*$big%gcd
    	echo "lcm:$lcm"    #输出lcm值
    	echo "gcd:$gcd"    #输出gcd值
    }
相关文章
相关标签/搜索