Shell Programming Experiment of Linux Programming Technology

Shell scripting of Linux programming technology

purpose:

  1. Master simple Shell programming
    2. Master the use of Shell variables
  2. Master the use of Shell expressions
  3. Master the use of Shell flow control statements
  4. Familiar with the execution and tracking of Shell programs
    Topics:
    1. Write script dic.sh, create directories in batches under the directory / home / test / scripts: dir + serial number (such as dir1, dir2, dir3, etc.), and create files in each directory : Login name + directory name, and give the file owner: read + write + execute permissions; users in the same group: read + execute permissions; other users: read + execute permissions
#!/bin/bash
cd /home/test/scripts
pwd
for i in $(seq 1 10)
do
	mkdir dir$i
	cd dir$i
	touch test+dir$i
	chmod 755 test+dir$i
       more test+dir$i
	cd /home/test/scripts
done

Results:
Insert picture description here
2. Write the script sum.sh, calculate the sum from 1 to 100 and return the result.

#!/bin/bash
#Get the sum of 1 to 100
sum =0
for((i=1;i<=100;i++))
do
	sum=$(($sum+$i))
done
echo"The total number is $sum."

Results:
Insert picture description here
3. Write a script back.sh to automatically archive all files in the / home / test / scripts directory. The name of the archive file is as follows: back-YYYY-MM-DD; save in the / home / test / backup directory.

#!/bin/bash
dirname=/home/test/backup
if [ ! -d $dirname ];
then
mkdir $dirname
fi
time=$(date "+%Y-%m-%d")
tar cvf back-$time.tar -c /home/test/scripts/*
mv back-$time.tar $dirname

Results:
Insert picture description here
4. There is a server software named: testd, write a script to judge whether the software is running, if not, start the process.

#!bin/bash
ps -fe|grep testd|grep -v grep
if [ $? -ne 0 ];
then

echo "starting process"

else
echo "process exist"
fi

result:
Insert picture description here

Published 16 original articles · Like1 · Visits 180

Guess you like

Origin blog.csdn.net/weixin_44931542/article/details/105174431