Translate page

Thursday, January 20, 2022

Unix For VLSI Industry - Part 2b - Assignment 2 - SHELL Scripting


SHELL Scripting based Assignment

Shell Scripting is going to easy your daily work - specially repeated work. In the previous Article we have discussed the UNIX command and first assignment - Now it's time to use the scripting language to automate our work around the Unix command. There are other scripting languages also like TCL / PERL and Python but in this article we will talk about the Assignments or say Practice questions related to SHELL Scripting. You can use Google for now to learn the scripting part and then test your understanding using below questions.

Operation Over File or Directory

  1. Write a shell script to call another script in it which should call another script inside it.
  2. Write a shell script to call another script if it exists. If not, print “file not found”.
  3. Write a shell script to call another script if you have permission to call it.
  4. Write a shell script in bash to traverse a list in bash.
  5. Write a shell script to check if a given file is a regular file or a directory if it exists.
  6. Modify the script written in Q-5 to get file name as argument to the script instead of user typing it at run time.
  7. Modify the script written in Q-6 to get the unlimited number of arguments as file name.
  8. Modify the script written in Q-5, if it is a directory, exit with a 1-exit status. If it is some other type of file, exit with a 2-exit status.
  9. Write a shell script to check if a given file is a directory and return 0 exit status and return non-zero exit code if it is not a directory.
  10. Write a shell script to count the number of directories in the given path.
  11. Write a shell script to count the number of files in the given path.
  12. Write a shell script to draw a tree-like structure describing the hierarchy of directories and files for a given path.
  13. Write a shell script to print the name of files colon separated along with their type and modified dates.
  14. Consider the multiple files in your PWD. Write a shell script to rename all the files with the extension *.sh and ask the user to add a specific prefix in the name of the file , else add “vlsi_expert_”. Remember to have unique names for all files.
  15. Write a shell script to change the extension of all files from *.sh to *.txt. If no file with the extension *.sh found on a given path, then exit script with non-zero exit status.

Execute Unix Command using SHELL Scripting

  1. Write a shell script to execute the command "catabc". Report “command succeeded” if the command returns a 0 exit and exit your script with a 0 exit. Report “command failed” if the command returns a 1 exit and exit your script with a 1 exit.
  2. Write a shell script to count the number of lines of a text file.
  3. Write a shell script to collect all files containing “VLSI Expert” in it.
  4. Write a shell script to get all the lines of a file containing “pass” in it.
  5. Let us suppose, you have created an application or a software and for some specific reason you do not want to run that application. Then, create a script to give proper explanation for not executing the application.
  6. Write a shell script to see current date, time, username, and current directory.
  7. When the terminal is opened, by default the present working directory is your home directory. Make some changes so that the default pwd should be ~/work directory for all the terminals that are opened.
  8. Whenever you open a terminal, it should display a message like “Welcome "{your-name}" to the world of Linux Scripting!”.

Printing and getting data from user using Command line options

  1. Write a shell script to print even numbers between 1 and 100.
    • Modify script written in Q-21 to display the output in the following manner:
      • 3 9 12 15 18 … 10 numbers on one line and then next 10 numbers on the next line and so on.
    • Now, Modify script written to ask the user for the range instead of the range 1 to 100 using read command. Put a check that the min range is less than the max range.
    • Now, Further Modify script to allow the user to specify the range as arguments to your script file at run time. Your script should then print the even numbers falling in this range [min:max].
    • Modify script written in Q-24 to check that only two arguments are mandatory otherwise print an appropriate help message as printed by any other Linux command.
  2. Write a shell script to find out the biggest number from the given three numbers. The numbers are supplied as command line parameters. Print error if sufficient arguments are not supplied.

Print Patters on the terminal

  1. Write a shell script to print below patterns:

This assignment is for your practice - you can practice on these scripting questions and later it will help you a lot in the Industry.
.
Stay Tune and Happy Learning. We discuss more in next Assignment.:)

Must Read Article

Related Posts Plugin for WordPress, Blogger...