Advanced C_Lecture 3 Shell command exercises

Determine the number of ordinary files and the number of directory files in the home directory

#!/bin/bash
a=(`ls -la ~/|cut -d r -f 1|grep -i d`)
b=(`ls -la ~/|cut -d r -f 1|grep -i -`)
echo "目录文件个数:${#a[*]}" 
echo "普通文件个数:${#b[*]}"

Enter a file name to determine whether it is a shell script file. If it is a script file, determine whether it has executable permission. If it has executable permission, run the file. If not, add executable permission to the file.

#!/bin/bash
read -p "请输入一个文件 " name
ret=`expr index $name \.`
r=`expr substr $name $((ret+1)) 2`
if [[ $r = sh ]]
then
	if [ -x $name ]
	then
		./$name
	else 
		chmod +x $name
	fi
fi

Enter two file names in the terminal to determine which file is updated

#!/bin/bash
if [ $1 -nt $2 ]
then
	echo "$1文件比$2更新"
else
	echo "$2文件比$1更新"
fi

The terminal enters the user to determine whether the user exists, if not, add the user

#!/bin/bash
read -p "please input user  " user

a=`grep -w $user /etc/passwd|cut -d / -f 2`
if [ "home" = "$a" ]
then
	echo "exists"
else
	sudo adduser $user
fi

Enter student grades, judge grades, A[100,90), B[90,80), C[80,70), D[70,60)

#!/bin/bash
read -p "please input a number  " num 
if [ $num -gt 90 ] && [ $num -le 100 ]
then
	echo "A"
elif [ $num -gt 80 ] && [ $num -le 90 ]
then
	echo "B"
elif [ $num -gt 70 ] && [ $num -le 80 ]
then
	echo "C"
elif [ $num -gt 60 ] && [ $num -le 70 ]
then
	echo "D"
else
	echo "E"
fi

Write a shell script to get the current user name, user id and working path

#!/bin/bash
echo `whoami`
echo `id -u`
echo $PATH

Count the number of files starting with P or p in the /etc directory

pending...

Guess you like

Origin blog.csdn.net/MaGuangming001/article/details/132119758