In: Computer Science
Linux Script
Convert String (Part 1)
Write a simple shell script that takes a permission string expressed as -rwxrwxrwx and prints out whether or not theobject is a directory, file or link. The string is read in from standard input.
Convert String (Part 2)
Modify the script so its able to print out the octal permission which the string represents along with the file type. This is in addition to part 1.
Convert String (Part 3)
For the script in Part 2, modify the script so it can read in as many strings till the end of file is reached.
Convert String (Part 4)
Modify the script so it reads the output of ls -la, converts the permission string into octal, prints out the type of object and also the name of the file.
Permission.sh:
#!/bin/bash
filetype(){ #Function for checking filetype
if [ $1 == "-" ] #Check if character matches
then
f_type="file" #Assign type if it
does
else
if [ $1 == "d" ]
then
f_type="directory"
else
if [ $1 == "l" ]
then
f_type="link"
fi #End if else
fi
fi
}
stringtobinary(){ #Convert rwx to binary
binary=""
if [ ${1:0:1} == "r" ] #Check if first character is
r
then
binary="${binary}1" #Append 1 if it
matches otherwise 0
else
binary="${binary}0"
fi
if [ ${1:1:1} == "w" ] #Check if second character is
w
then
binary="${binary}1" #Append 1 if it
matches otherwise 0
else
binary="${binary}0"
fi
if [ ${1:2:1} == "x" ] #Check if third character is
x
then
binary="${binary}1" #Append 1 if it
matches otherwise 0
else
binary="${binary}0"
fi
return $binary
}
binarytooctal(){ #Convert binary to octal
oct=2
h=`expr ${1:0:1} \* $oct \* $oct` # First digit x
8^2
d=`expr ${1:1:1} \* $oct` # Second digit x 8^1
u=`expr ${1:2:1} + 0` # Third digit x 8^0
res=$((h + d + u)) # Add all variables
return $res #Return result
}
octal(){
stringtobinary ${1:1:3} #Invoke function
user=$? #Store returned value
stringtobinary ${1:4:3}
group=$?
stringtobinary ${1:7:3}
other=$?
if [ $user != "0" ] #Check if binary representation is
only 0
then
binarytooctal $user #Invoke
function
user=$? #Store returned value
fi
if [ $group != "0" ]
then
binarytooctal $group
group=$?
fi
if [ $other != "0" ]
then
binarytooctal $other
other=$?
fi
octal_res="${user}${group}${other}" #Concatenate
result to form a string
}
ans="y" #initialize global variables
f_type=""
octal_res=""
while [ $ans == "y" ] #Loop until user doesn't exit
do
echo "1.Print permission filetype"
echo "2.Print octal permission and filetype"
echo "3.Take multiple permissions, print octal
permissions and filetypes"
echo "4.ls -la to octal permissions"
echo "Enter your choice:"
read choice #Take input from user
if [ $choice == "1" ] || [ $choice == "2" ] #Check if
choice is 1 or 2
then
echo "Enter the permission" #Print
statement
read string #Read input
identifier=${string:0:1} #Take the
first character from the string
filetype $identifier $string
#Invoke function and pass 2 arguments
echo "Object Type: $f_type"
#Display result
if [ $choice == "2" ]
then
octal
$string
echo "The octal
representation is ${octal_res}"
fi
else
if [ $choice == "3" ]
then
echo "1.Read permissions from
file"
echo "2.Enter permissions
manually"
echo "Enter your choice:"
read choice
if [ $choice == "1" ]
then
echo "Enter the
filename:"
read
filename
while read -r
line #Read input file line by line
do
filetype ${line:0:1} $line
octal $line
echo "$line $octal_res
${f_type}"
done <
"$filename" #Execute loop until end of file is reached
else
if [ $choice == "2" ]
then
echo "Enter the
number of permissions"
read num
for
((i=1;i<=num;i++))
do
#For each iteration take input from user
echo "Enter the permission:"
read string
identifier=${string:0:1}
filetype $identifier $string
octal $string
echo "$string $octal_res
${f_type}"
done
fi
fi
else
if [ $choice == "4" ]
then
echo "Permission
Octal Filetype Filename"
ls -la| while read line #Read ls
-la as list of lines
do
fields=($line)
#Separate each line into tokens
if [
${fields[0]} != "total" ]
then
permission=${fields[0]} #Store permission
filetype ${permission:0:1} ${fields[0]}
octal ${fields[0]}
echo "${permission}
${f_type} ${octal_res} ${fields[8]}"
fi
done
fi
fi
fi
echo "Do you want to continue?(y/n)"
read ans
done #End while loop
Output: