Talk:SPR720 BASH Scripting Lab

From CDOT Wiki
Jump to: navigation, search

Write scripts to do three of the following tasks:

  1. Loop through the files in your home directory, and for each readable file, ask whether the file should be printed, mailed to you, or ignored (P/M/I) and then take the appropriate action.
  2. Display the longest and shortest usernames on the system (usernames are in the first field in /etc/passwd).
  3. Count the number of users with user IDs between 500 and 10000 on the system (user IDs are the third field in /etc/passwd).
  4. Display the names of any filesystems which have less than 10% free space available (see the df command for this information).
  5. Ask the user for an e-mail address, then send the output of the dmesg command to that address.
  6. Count the number of files in the user's home directory which are not readable.
  7. For each directory in the $PATH, display the number of executable files in that directory. 


BossaNesta

   1. Loop through the files in your home directory, and for each readable file, ask whether the file should be printed, 
mailed to you, or ignored (P/M/I) and then take the appropriate action.


   2. Display the longest and shortest usernames on the system (usernames are in the first field in /etc/passwd).

LONGEST_NAME=""
SHORTEST_NAME="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
for USERNAME in $(cat /etc/passwd | cut -d: -f 1)
do
# 	echo "USERNAME: " $USERNAME
	if [ $(echo "$USERNAME" | wc -c) -ge $(echo $LONGEST_NAME | wc -c) ]
	then
		LONGEST_NAME=$USERNAME
#		echo $LONGEST_NAME
	elif [ $(echo "$USERNAME" | wc -c) -lt $(echo $SHORTEST_NAME | wc -c) ] 
	then
                SHORTEST_NAME=$USERNAME
#                echo "short" $SHORTEST_NAME
# 		echo "current" $USERNAME
fi
done
echo "The Longest Name is:	" $LONGEST_NAME
echo "The Shortest Name is:	" $SHORTEST_NAME



   3. Count the number of users with user IDs between 500 and 10000 on the system 
(user IDs are the third field in /etc/passwd).

UID_COUNT=0
for USERID in $(cat /etc/passwd | cut -d: -f 3)
do
# 	echo "USERID:" $USERID
	if [ "$USERID" -ge 500 -a "$USERID" -lt 10000 ]
	then
		((UID_COUNT++))
		echo "Current Count: " $UID_COUNT
	fi
done
echo "Total count of user ID between 500 to 10000 is:	" $UID_COUNT


   4. Display the names of any filesystems which have less than 10% free space available (see the df command for this information).


   5. Ask the user for an e-mail address, then send the output of the dmesg command to that address.

 echo "Type your email address, we'll send u the dmesg message: "
 read EMAIL_ADDRESS
 echo "address" $EMAIL_ADDRESS
 dmesg | mail -s "............Output of the DMESG command.........." "$EMAIL_ADDRES"


   6. Count the number of files in the user's home directory which are not readable.


   7. For each directory in the $PATH, display the number of executable files in that directory. 

ALL_TOTAL=0
for DIR in $(echo $PATH | tr : "\n")
do
        TOTAL=0
        echo
        cd $DIR
        echo "Current dir: "$PWD
        for FILES in $(ls $DIR)
        do
#                echo "FILES: " $FILES
                if [ -x $FILE -a -f $FILE ]
                then
                        ((TOTAL++))
                fi

        done
        echo "current count:" $TOTAL
        ALL_TOTAL=$(($ALL_TOTAL+$TOTAL))
done

echo "All current user executable count are: $ALL_TOTAL"