All pastes #913674 Raw Edit

Miscellany

public text v1 · immutable
·published 2008-02-22 06:59 UTC
rendered paste body
#!/bin/bash

################################################################################
# Filename: lsch.sh
# Version: 1.0
# Author Name: Marc Tessier
# Student Number: 040576114
# Course Name: UNIX/Linux Shell Programming
# Course Number: CST8129
# Lab Section: 010
# Assignment Number: 2
# Assignment Name: lsch
# Due Date: February 22, 2008
# Submission Date: February 22, 2008
# Professor's Name: Shawn Unger
# Purpose: Enhance the basic ls behaviour and add the ability to change
# the permissions of a file(s)/directory(ies) if desired by the user
################################################################################

Main () {
################################################################################
########################### Variable Declarations ##############################
################################################################################
declare FileSize
declare GroupName
declare UserName
declare FileName=$1
declare User_Per
declare Group_Per
declare Other_Per
declare File_Per
declare File_Dir
declare Mod_Date
declare User_Sel
declare OctalValue
declare bValid
declare User_Per1
declare User_Per2
declare User_Per3
declare FileDir

################################################################################
# Function Name: OctalConcert
# Purpose: Concert user input permissions to octal values
# Function Inputs: User_Per, Group_Per, Other_Per
# Function Outputs: OctalValue
# Version: 1.0
# Author Name: Marc Tessier
################################################################################
OctalConvert () {
OctalValue=($(echo $1 | tr "[:upper:]" "[:lower:]"))
 case "$OctalValue" in
  ---)
   OctalValue=0 ;;
  --x)
   OctalValue=1 ;;
  -w-)
   OctalValue=2 ;;
  -wx)
   OctalValue=3 ;;
  r--)
   OctalValue=4 ;;
  r-x)
   OctalValue=5 ;;
  rw-)
   OctalValue=6 ;;
  rwx)
   OctalValue=7 ;;
 esac
}

################################################################################
################# File permission format for correct output ####################
################################################################################
File_Per=($(ls -ld "$FileName" | sed 's/^.//p'))
File_Per=($(echo $File_Per | cut -c 1-3):$(echo $File_Per | cut -c 4-6):$(echo $File_Per | cut -c 7-9))
File_Per=($(echo $File_Per | sed 's/r/Read/g'))
File_Per=($(echo $File_Per | sed 's/w/~Write/g'))
File_Per=($(echo $File_Per | sed 's/x/~Execute/g'))
File_Per=($(echo $File_Per | sed 's/-//g'))

################################################################################
########################## Variable Initialization #############################
################################################################################
FileDir=($(ls -ld "$FileName" | cut -c 1))
Mod_Date=$(ls -ld --time-style="+%B %d, %Y at %H:%M" "$FileName" | cut -d " " -f 6-10)
UserName=($(ls -ld "$FileName" | cut -d " " -f 3))
GroupName=($(ls -ld "$FileName" | cut -d " " -f 4))
FileSize=($(ls -ld "$FileName" | cut -d " " -f 5))
User_Per=($(ls -ld "$FileName" | cut -c 2-4))
Group_Per=($(ls -ld "$FileName" | cut -c 5-7))
Other_Per=($(ls -ld "$FileName" | cut -c 8-10))
User_Per1=($(ls -ld "$FileName" | cut -c 2-4))
Group_Per1=($(ls -ld "$FileName" | cut -c 5-7))
Other_Per1=($(ls -ld "$FileName" | cut -c 8-10))

################################################################################
########################## Basic Attributes Section ############################
################################################################################
if [[ $FileDir == "d" ]]		# Check if file is a directory
then
	echo "Directory: "$FileName""	# Output Directory filename
else
	echo "File: "$FileName""	# Output file filename
fi
echo "User: $UserName"			# Output Owner
echo "Group: $GroupName"		# Output Group
if [[ $FileDir == "d" ]]		# Check if file is a directory
then
	echo "Size: N/A"		# Size directory size to N/A
else
	echo "Size: $FileSize bytes"	# Output file size
fi

################################################################################
####################### Permission Attributes Section ##########################
################################################################################
echo -n "Permissions: User: "
echo $(echo $File_Per | sed 's/~/ and /g' | cut -d ":" -f 1)	# Output User permissions
echo -n "            Group: "
echo $(echo $File_Per | sed 's/~/ and /g' | cut -d ":" -f 2)	# Output Group permissions
echo -n "            Other: "
echo $(echo $File_Per | sed 's/~/ and /g' | cut -d ":" -f 3)	# Output Other permissions
echo -n "Modification Date: "
echo $Mod_Date				# Output Modification date
echo ""

################################################################################
########################## Permission Edit Section #############################
################################################################################
read -p "Would you like to change the permissions on this file [Y/N]
any other characters will be assume (N): " User_Sel	# Get user selection

if [[ $User_Sel == "Y" || $User_Sel == "y" ]]		# Check if YES
then
	echo "Valid Permission are: RWX or an octal value, a dash (-) will"
	echo "                      leave the permission unchange"
	echo ""

############################### User Permissions ###############################

	bValid=0		# Set valid flag to false
	while (( $bValid==0 ))	# While entry is not valid
	do	
		read -p "New User Permissions (Currently $User_Per1): " User_Per
		if ( echo $User_Per | grep "^[0-7]$" > /dev/null )	
		then			# If Valid octal
			bValid=1	# Set Valid flag to true
		elif ( echo $User_Per | grep -i "^[r\-][w\-][x\-]$" > /dev/null )
		then			# If Valid Character
			bValid=1	# Set Valid flag to true
		elif [[ $User_Per == "-" ]]
		then			# If dont change permissions character
			bValid=1	# Set Valid flag to true
		else 			# Reprompt for valid entry
			echo "Invalid Permissions, please use RWX or an octal value (0-7)"
		fi
	done

	if [[ $User_Per == "-" ]]	# If dont change permissions character
	then
		User_Per=($(ls -ld "$FileName" | cut -c 2-4))
		OctalConvert $User_Per
		User_Per=$OctalValue	# Convert unchanged permission to octal
	else			
		OctalConvert $User_Per
		User_Per=$OctalValue	# Convert new permission to octal
	fi

############################### Group Permissions ###############################

        bValid=0		# Set valid flag to false
        while (( $bValid==0 ))	# While entry is not valid
	do
		read -p "New Group Permissions (Currently $Group_Per1): " Group_Per
		if ( echo $Group_Per | grep "^[0-7]$" > /dev/null )
		then			# If Valid Octal
			bValid=1	# Set Valid flag to true	
		elif ( echo $Group_Per | grep -i "^[r\-][w\-][x\-]$" > /dev/null )
		then			# If Valid Character
			bValid=1	# Set Valid flag to true
		elif [[ $Group_Per == "-" ]]
		then			# If dont change permissions character
			bValid=1	# Set Valid flag to true
		else			# Reprompt for valid entry
			echo "Invalid Permissions, please use RWX or an octal value (0-7)"
		fi
	done

	if [[ $Group_Per == "-" ]]	# If dont change permissions character
	then
		Group_Per=($(ls -ld "$FileName" | cut -c 5-7))
		OctalConvert $Group_Per
		Group_Per=$OctalValue	# Convert unchanged permissions to octal
	else
		OctalConvert $Group_Per	
		Group_Per=$OctalValue	# Convert new permissions to octal
	fi

############################### Other Permissions ###############################
	bValid=0		# Set Valid Flag to flase
	while (( $bValid==0 ))	# While entry is not valid
	do
		read -p "New Other Permissions (Currently $Other_Per1): " Other_Per
		if ( echo $Other_Per | grep "^[0-7]$" > /dev/null )
		then			# If Valid Octal
			bValid=1	# Set Valid Flag to true
		elif ( echo $Other_Per | grep -i "^[r\-][w\-][x\-]$" > /dev/null )
		then			# If Valid Character
			bValid=1	# Set Valid Flag to true
		elif [[ $Other_Per == "-" ]]
		then			# If dont change permissions character
			bValid=1	# Set Valid flag to true
		else			# Reprompt for valid entry
			echo "Invalid Permissions, please use RWX or an octal value (0-7)"
		fi
	done

        if [[ $Other_Per == "-" ]]	# If dont change permissions character
	then
		Other_Per=($(ls -ld "$FileName" | cut -c 8-10))
		OctalConvert $Other_Per
		Other_Per=$OctalValue	# Convert unchanged permissions to octal
	else
		OctalConvert $Other_Per
		Other_Per=$OctalValue	# Convert new permissions to octal
	fi

################################# chmod command #################################
	File_Per=($(echo $User_Per $Group_Per $Other_Per | sed 's/ //g'))
	chmod -R $File_Per "$FileName"	# chmod new value filename
	echo "New File Data:"
	ls -ld "$FileName"		# Output new attributes
fi
}
counter=0				
counter_pointer=1
while [ $counter -lt $# ]		# While there is still files
do
	obj=`eval echo \\$$counter_pointer`	# Make object = that file
	Main "$obj"				# Pass the object into main
	(( counter=counter+1 ))			# Increment
	(( counter_pointer=counter_pointer+1 ))
done