0% found this document useful (0 votes)
4 views4 pages

Shell Script for File Management Tasks

The document outlines a shell script that allows users to perform various file and directory operations, including creating a file to store personal information, displaying its contents, deleting directories, sorting a numeric file, and changing file permissions. It provides a menu-driven interface where users can choose an option to execute the corresponding command. The script includes error handling for invalid choices.

Uploaded by

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

Shell Script for File Management Tasks

The document outlines a shell script that allows users to perform various file and directory operations, including creating a file to store personal information, displaying its contents, deleting directories, sorting a numeric file, and changing file permissions. It provides a menu-driven interface where users can choose an option to execute the corresponding command. The script includes error handling for invalid choices.

Uploaded by

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

Script No .

Definition:
Write a shell script to execute following commands
• Create a file called text and store name, age and address in it.
• Display the contents of the file text on the screen.
• Delete the directories mydir and newdir at oneshot.
• Sort a numeric file?
• Change the permissions for the file newtext to666.
Script:
echo “1. Create a file called text and store name,age and address in it.”
echo “2. Display the contents of the file text on the screen.”
echo “3. Delete the directories mydir and newdir at one shot.”
echo “4. Sort a numeric file”
echo “5. Change the permissions for the file newtext to 666.”
echo “enter your choice”
read ch
case $ch in
1) echo “Create a file called text and store name,age and address in it.”
echo “Enter the filename”
read fn
cat > $fn ;;
2) echo “Display the contents of the file text on the screen.”
cat $fn ;;
3) echo “Delete the directories mydir and newdir at one shot.”
rmdir mydir newdir ;;
4) echo “Sort a numeric file”
sort -n filename ;;
5) echo “Change the permissions for the file newtext to 666.”
chmod 666 newtext ;;
*) echo “Invalid choice” ;;
Esac

OR

echo "1. Create file called text and store name, age and address in it."
echo "2. Display the contents of the file text on the screen."
echo "3. Delete the directories mydir and newdir at one shot."
echo "4. Sort a numeric file"
echo "5. Change the permissions for the file newtext to 666."
echo "enter your choice"
read ch
case $ch in
1) echo "Create a file called text and store name, age and address in it."
echo "Enter the filename"
read fn
echo "Enter name : "
read name
echo "Enter age : "
read age
echo "Enter address : "
read adr
echo -e "Name : $name\nAge: $age\nAddress" > "$fn";;
2) echo "Displaying the contents of the file text on the screen. "
cat [Link];;
3) echo "Delete the directories mydir and newdir at one shot."
rmdir mydir newdir;;
4) echo "Sort a numeric file"
sort -n filename;;
5) echo "Change the permissions for the file newtext to 666."
chmode 666 newtext;;
*) echo "INvalide choice";;
esac

OR
# Make sure u have students stored data file

echo "1) Create file"


echo "2) Display contents of file"
echo "3) Delete directories and make directories"
echo "4) sort file"
echo "5) Change permission of file next to 666"
echo "Enter your choice"
read ch

case $ch in
1) cat>[Link];;
2) cat [Link];;
3) rm -r mydirnewdir;;
4) sort -n [Link];;
5) chmod 666 [Link];;
*) echo "Invalid choice"
esac

Common questions

Powered by AI

The shell script in Source 1 is designed to provide users with a menu-driven interface to execute a series of tasks. It uses the 'case' statement to handle different user choices. For creating a file and storing details, it prompts for a filename and user inputs, which it then writes to the file. It displays a file's contents using 'cat', deletes directories using 'rmdir', sorts a numeric file using 'sort -n', and changes permissions with 'chmod 666'. This structured approach makes executing these tasks more user-friendly by allowing selection through prompts .

A 'case' statement in shell scripting serves as a control flow mechanism to execute different blocks of code based on user input, which matches specified patterns. In Source 1, the 'case' statement presents users with options, reads their input, and executes corresponding commands. This structure simplifies scripting by allowing a clear, concise method of handling multiple user requests, enhancing script maintainability and readability .

Improper handling of user inputs can lead to script errors, unintended commands execution, or security vulnerabilities such as code injection. The script in Source 1 mitigates these risks by predefining acceptable choices using a 'case' statement and handling invalid inputs with an error message, which prevents exploitation and guides users to valid script operations .

Using shell scripts like the one in Source 1 for educational purposes introduces students to fundamental programming concepts such as control flow, conditional structures, and basic file operations. These scripts provide hands-on experience with real-world applications, fostering a practical understanding of how scripts automate tasks, manage inputs, and handle files. Consequently, they support the development of problem-solving skills and the teaching of best practices in scripting, which are crucial in programming education .

'Rmdir' in the script is significant for removing empty directories, namely 'mydir' and 'newdir'. Its inclusion automates directory cleanup, reducing manual file management efforts. However, 'rmdir' will fail if the directories are not empty, which highlights the importance of ensuring directories contain no files before execution. This failure ensures directories are not accidentally deleted along with their contents, thereby preventing data loss .

The shell script handles invalid user input by including a default case in the 'case' statement which outputs 'Invalid choice'. This feature is crucial as it provides feedback to the user when an unrecognized option is selected, thus preventing script execution errors and guiding the user back to valid choices .

Changing file permissions using 'chmod' is critical for ensuring that the file has the correct access rights, which can protect sensitive information and control how the file can be used. By setting permissions to 666, as seen in the script, the owner, group, and others can read and write the file, but not execute it. This controls access and prevents unauthorized modifications or misuse .

The script automates routine tasks such as file creation, content display, directory deletion, numeric sorting, and permission changes. This automation reduces manual input, minimizes human error, and saves time, allowing users to efficiently manage files and processes. Automating these tasks increases productivity and ensures consistent execution of operations .

The script ensures data persistence by asking the user to create a file and then prompts for and captures inputs for name, age, and address. This information is then written to the specified file using 'echo' and 'cat', ensuring it is stored on disk and remains accessible after the script execution finishes .

The 'sort -n' command is used to sort lines of a file numerically. This is particularly useful when dealing with data files where numerical sorting is necessary, such as sorting student scores or ages. By incorporating this function into the script, it allows users to organize their data in a logical order without needing to manually process the file contents, thereby enhancing the script's utility .

You might also like