Question

In: Computer Science

Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string...

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.

Solutions

Expert Solution

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:


Related Solutions

LINUX In Linux command line write a shell script ex1.sh that uses IF THEN to Prompt...
LINUX In Linux command line write a shell script ex1.sh that uses IF THEN to Prompt the user to "Enter a number between 1 and 10". (Hint: Use the 'echo' and 'read' commands in the script. See the slide about the 'read' command) If the number is less than 5, print "The number is less than 5" (Hint: You will read input into a variable; e.g. read NUM. In the IF statement, enclose $NUM in quotes; e.g. "$NUM". Also, remember...
Perl is a programming language that can be used on Linux. Write a Perl shell script...
Perl is a programming language that can be used on Linux. Write a Perl shell script named phone.pl that prompts the user to enter first or last or any portion of person’s name, so that can be found the appropriate entry in the phone directory file called “phones”. If the user tries to enter name as the argument on the command line, he/she will get a warning message “You need to provide name when prompted by this script!” If the...
Linux Sign into your lab account Write a bash shell script that does the following: Print...
Linux Sign into your lab account Write a bash shell script that does the following: Print out a friendly welcome message Ask the user for the name of their favorite animal Grep that animal from a text file noises.txt Tell the user what that animal says (i.e. what noise does it make) If the animal does not exist ask the user what noise the animal makes Store that new information in noises.txt
Design two shell programs working on Linux (Ubuntu) Design a shell script program, 1) reading given...
Design two shell programs working on Linux (Ubuntu) Design a shell script program, 1) reading given only two integer numbers from command line arguments and computing their multiplication. If two integer numbers are not given, print “Wrong Input” on your screen. Note that, the number of arguments is known when the script runs. Take a screenshot showing your shell program and its execution step. Design a shell program to remove all the shell programming files ending with sh on your...
Linux Shell Prpgramming 1.Write a quiz-script which will provide some statistics about a quiz results (summarize...
Linux Shell Prpgramming 1.Write a quiz-script which will provide some statistics about a quiz results (summarize how many good answers and how many wrong answers were provided). The script should use up to 3 execution parameters. When executed with two parameters (let’s call it interactive mode), it should assume, that the first file contains a list of questions and the second file contains correct answers to these questions. In such execution case your script should display questions one by one...
Write a bash shell script that takes exactly one argument, a file name. If the number...
Write a bash shell script that takes exactly one argument, a file name. If the number of arguments is more or less than one, print a usage message. If the argument is not a file name, print another message. For the given file, print on the screen its content.
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
Revision Question 1 on Linux. Please briefly explain the shell script given. a) You have just...
Revision Question 1 on Linux. Please briefly explain the shell script given. a) You have just logged in and have a directory called "images" in your home directory containing the following files: favicons login.png logo.png newlogo.png where "favicons is a directory and contains the files favicon.ico favicon.gif favicon.png favicon.jpg Describe the results you would expect when executing the following shell commands: i) ls images/*og* | wc -1 ii) ls -ld images/* iii) rmdir images/favicons iv) cp images/*/*png images v) rm...
(10pts) Write a shell script to display your name to the screen. (10pts) Write a shell...
(10pts) Write a shell script to display your name to the screen. (10pts) Write a shell script to take a directory as an argument and display the contents of that directory (10pts) Write a shell script that takes any command as a parameter and displays help on that command (e.g. the result of the execution of man <command>). (10pts) Write a shell script that requires two file names as parameters and copies content of one file into another without asking...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT