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:
?
- The characters entered do not match the name of the network card Norms, how to deal with them.
- 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:
?
- The script can take parameters or not
li>
- There can be multiple parameters, and each parameter must be a directory
- 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>
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:
?
- 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
- If you enter the same name, the output The number is still the result output when the name is entered for the first time
- The number that has been output before will not appear again next time
- 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