Shell exercise-18

Question requirements

prompt the user to enter the name of the network card, and then we use the script to output the ip of the network card, we need to consider the following questions:
?

  1. The characters entered do not match the name of the network card Norms, how to deal with them.
  2. The name conforms to the specification, but how to deal with it without this network card at all.

Reference answer

#!/bin/bash
ip add |awk -F': ''$1 ~ "^[1-9]" {print $2}'> /tmp/eth.list
while :
do
eths=`cat /tmp/eth.list |xargs`
read -p "Please input a if name(The eths is `echo -e "\033[31m$eths\033[0m"`): "eth
if [-z "$eth" ]
then
echo "Please input a if name."
continue
fi
if! grep -qw "$eth" /tmp/eth.list
then
echo "The if name is error."
continue
else
break
fi
done

if_ip()
{
ip add show dev $1 |grep 'inet' |awk'{print $2}'|awk -F'/''{print $1}' >/tmp/$1.txt
n=`wc -l / tmp/$1.txt|awk'{print $1}'`
if [$n -eq 0 ]
then
echo "There is no ip address on the eth."
else
echo "The ip addreess is:"
for ip in `cat /tmp/$1.txt`
do
echo -e "\033[33m$ip\ 033[0m"
done
fi
}

if_ip $eth

Question requirements

Write a script to achieve the following functions:
?

  1. The script can take parameters or not

    li>

  2. There can be multiple parameters, and each parameter must be a directory
  3. The script checks the number of parameters. If it is equal to 0, the current directory itself will be listed, otherwise the sub-parameters contained in each parameter will be displayed. content.

Reference answer

#!/bin/bash
if [$# -eq 0 ]
then
echo "current The file under the directory is: "
ls .
else
for d in [email protected]
do
if [-d $d ]
then
echo "There are these subdirectories under the directory $d:"
find $d -type d
else
echo "There is no such directory: $d"
fi
done
fi

Question requirements

Define a shell function that can accept two parameters and meet the following requirements:
?

< ol>

  • The first parameter is the URL, the file that can be downloaded, and the second parameter is the directory, which is the location to save after downloading
  • If the directory given by the user does not exist, the user will be prompted whether Create, if created, continue to execute, otherwise, the function returns an error value of 51 to the calling script
  • If the given directory exists, download the file. After the download command is executed, it will test whether the file is downloaded successfully or not. If If successful, return 0 to the calling script, otherwise, return 52 to the calling script
  • Refer to the answer

    #!/bin/bash
    if [$# -ne 2 ]
    then
    echo "You must enter two parameters. The first parameter is the URL and the second parameter is the directory."
    exit 1
    fi< br />
    if [! -d $2 ]
    then
    while :
    do
    echo "The second parameter you entered is not an existing directory .Do you want to create this directory? (y|n): "c
    case $c in
    y|Y)
    mkdir -p $2
    ;;
    n|N)
    exit 51
    ;;
    *)
    echo "Please enter y or n."
    continue
    ;;
    esac
    done
    else
    cd $2
    wget $1
    if [$? -eq 0 ]
    then
    exit 0
    else
    echo "download failed."
    exit 52
    fi
    fi

    Question requirements

    Write a guessing number script. When the number entered by the user is the same as the preset number (a number from 0-100 is randomly generated), exit directly, otherwise let The user keeps inputting, and prompts the user that the number is larger or smaller than the preset number.

    Reference answer

    #!/bin/bash
    n=$[$RANDOM%101]
    while :
    do
    read -p "Please enter a number from 0-100:" n1
    if [-z "$n1" ]
    then
    echo "You must enter a number."
    continue
    fi
    n2=`echo $n1 |sed's/[0-9]//g'`
    if [-n "$n2" ]
    then< br /> echo "The number you entered is not a positive integer."
    continue
    else
    if [$n -gt $n1 ]
    then
    echo "you The number you entered is too small, please try again."
    continue
    elif [$n -lt $n1 ]
    then
    echo "The number you entered is too big, please try again ."
    continue
    else
    echo "Congratulations, you guessed it!"
    break
    fi
    fi
    done

    Question requirements

    Writing a shell script can meet the following requirements:
    ?

    1. After executing the script, prompt for the name (in English, it can be large or small) Write letters and numbers without other special symbols), and then output a random number between 0-99, the script will not exit, continue to prompt to enter the name
    2. If you enter the same name, the output The number is still the result output when the name is entered for the first time
    3. The number that has been output before will not appear again next time
    4. When q or Q is entered, the script will exit.

    Reference answer

    #!/bin/bash
    f=/tmp/user_number.txt
    j_n()
    {
    while :
    do
    n=$[RANDOM%100]
    if awk'{print $2}' $f|grep -qw $n
    then
    continue
    else
    break
    fi
    done
    }

    while :
    do
    read -p "Please input a username: "u
    if [-z "$u" ]
    then
    echo "Please input a username."
    continue
    fi

    if [$u == "q"] || [$u == "Q" ]
    then
    exit
    fi

    u1=`echo $u|sed's/[a-zA-Z0-9]//g'`
    if [-n "$u1" ]
    then
    echo "you The username entered does not conform to the specification, the correct username should be a combination of uppercase and lowercase letters and numbers"
    continue
    else
    if [-f $f ]
    then
    u_n=`awk -v uu=$u'$1==uu {print $2}' $f`
    if [-n "$u_n" ]
    then
    echo "user The number corresponding to $u is: $u_n"
    else
    j_n
    echo "The number corresponding to user $u is: $n"
    echo "$u $n" >> $f
    fi
    else
    j_n
    echo "The number corresponding to user $u is: $n"
    echo $u $n >> $f
    fi
    fi
    done

    WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
    SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5513 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

    Leave a Comment

    Your email address will not be published.