Linux experiment six shell programming (loop)
1. Use shell to write multiplication table program
vi chengfabiao.sh
Then press the lowercase i key to edit
#!/bin/bash
for ((i=1;i<10;++i))
do
for ((j=1;j<=i;j++))
do
#-n是不自动换行,-e激活双引号内的转义字符
echo -ne "$i*$j=$((i*j))\t"
done
echo #这个echo是为了换行使用
done
After editing, press esc to exit the editing mode, then press: wq to save and exit.
Use chmod 755 chengfabiao.sh to give the file executable permissions
sh chengfabiao.sh to execute the file
2. Write a script program that can take different actions according to the input command line parameters: if it is a directory, list the files in the directory; if it is an executable file, use a shell to execute it; if it is a readable file, then Display its content in split screen.
vi panduan.sh
#!/bin/bash
if [ -e $1 ]
then
if [ -d $1 ]
then
ls $1
elif [ -x $1 ]
then
sh $1
elif [ -r $1 ]
then
more $1
fi
else
echo "输入的参数无效!"
fi
Use the q key to exit split-screen viewing mode
3. Write a shell script to create the directory /userdate, create 100 directories in batches under this directory, namely user1~user100, and set the permissions of each directory. Among them, the permissions of other users are: read; the permissions of the file owner are : Read, write, execute; the group permissions of the file owner are: read, write.
vi mulu,.sh
#!/bin/bash
mkdir /userdate
cd /userdate
for ((i=1;i<=100;i++))
do
mkdir user$i
chmod 764 user$i
done