0% found this document useful (0 votes)
16 views2 pages

Linux Final Assignments

The document outlines various shell scripting tasks including error handling, creating a simple calculator, listing directory files, implementing a countdown timer, counting lines in a text file, checking file existence, and determining file types. It provides hints and commands such as 'trap', 'read', 'ls', and 'wc -l' for each task. Additionally, it emphasizes the importance of error handling and variable declaration in scripts.

Uploaded by

pathakpritee20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Linux Final Assignments

The document outlines various shell scripting tasks including error handling, creating a simple calculator, listing directory files, implementing a countdown timer, counting lines in a text file, checking file existence, and determining file types. It provides hints and commands such as 'trap', 'read', 'ls', and 'wc -l' for each task. Additionally, it emphasizes the importance of error handling and variable declaration in scripts.

Uploaded by

pathakpritee20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

• Enable Strict error handling at the beginning: -

• Create an Error handling script using function

– Write a function to handle error

– # Function to handle errors

– error_handler()

– {

– echo "Error: An error occurred in the script on line $1."

– exit 1

– }

• Use trap command captures errors and executes the error_handler function when an error
occurs.

– # Trap errors and call the error handler

– trap 'error_handler $LINENO' ERR

• Declare two variables: -

– SOURCE_FILE=“Give some value“

– TARGET_DIR =“Give some value“

• Use If

2.

• Simple Calculator:

– Create a script that takes two numbers and an arithmetic operator (+, -, *, /) as input
and performs the corresponding operation.

– Hint: Use read and case statements.

3.

• Directory Listing:

– Write a script that lists all the files in a specified directory and their sizes.

Hint: Use ls and du commands.


4.

• Countdown Timer:

– Create a script that takes a number as input and counts down to zero, displaying
each number.

– Hint: Use while loop and sleep command.

• 5. Text File Line Count:

– Write a script that counts the number of lines in a specified text file and prints the
result.

– Hint: Use wc -l command.

• 6. Check file existence:

– Create a script that checks if a particular file exists.

– Hint: Use If statement.

• write a shell script that prompts the user for a name of a file or directory and reports if it is a
regular file, a directory, or another type of file. Also perform an ls command against the file
or directory with the long listing option: -

– Hint: Start script with enter file path, then read the file, Use –f in If statement, use
elseif with [-d”$FILE] (this is to display if it’s a directory) else display, file is of some
another type & then display list entire list.

• Sum of Numbers with Error Handling:

– Hint: Use If, Else If, assign sum=0 in the beginning

You might also like