Linux programming _Shell script practice questions

1. Write a shell script to calculate the sum of 1~100.

#! /bin/bash

sum=0
for i in `seq 1 100`;do
        sum=$[$i+$sum]
done
echo $sum

 

2. Write a shell script, enter a number n and calculate the sum of 1~n.

Requirement: If the entered number is less than 1, re-enter until the correct number is entered.

#! /bin/bash

n=0
while [$n -lt "1"];do
        read -p "Please input a number:"n
done

sum=0
for i in `seq 1 $n`;do
        sum=$[$i+$sum]
done
echo $sum

 

3. Write shell scripts to create users user_00, user_01...user_99 in batches.

Requirements: All users belong to the users group.

#! /bin/bash

groupadd users
for i in `seq -w 0 99`;do
        uesradd -g users user_0$i4
done

 

 

4. Write shell scripts to add users jsj01-jsj09 and jsj10-jsj99 in batches.

#! /bin/bash

for((i=1;i<20;i++));
do
if(i<10);then
jsj="jsj0$i";
else
jsj="jsj$i";
fi
useradd $jsj
done

 

5. Write a shell script, which requires the following functions: when a program is executed, the program will let the user choose boy or girl; if the user enters B or b, it will display: He is a boy; if the user enters When G or g, it will display: He is a girl;

If it is any character other than B/b/G/g, it will display: I don't know.

#! /bin/bash

echo -n your choice:
read choice
case $choice in
[g,G]
echo "She is a girl."
;;
[b,B]
ehco "He is a boy."
;;
*)
echo "I don't know."
;;
esac

 

6. Write a shell script to realize addition, subtraction, multiplication and division between two variables.

#! /bin/bash

read -p "imput num1:"a
read -p "input num2:"b
read -p "input operator:"o
case $o in
+)let "res=a+b"
echo $res;;
-)let "res=a-b"
echo $res;;
/)awk'BEGIN{printf %.2f\n",'$a'/'$b'}';;
*)let "res=a*b"
echo $res;;
esac

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325253964&siteId=291194637