Friday 4 December 2015

File Handling in Linux -BASH SCRIPT

File handling can easily be carried out by an administrator in Linux using Shell Scripts or BASH programming. Below is the code for a script for file handling in Linux.


Code
Linux-Shell-Scripting-Bash-File-Handling
Shell Scripting

#!/bin/bash
echo "Program by Abhishek Munagekar" 
echo "For more visit www.facebook.com/prgwonders"
echo "Blog:-www.prgwonders.blogspot.com"
echo "shell script to create ,rename and delete a file"
while true
do
echo "enter your choice"
echo "1.create file  2.rename file 3.delete file 4.exit"
read choice
case $choice in
1)
echo "enter name of file to create"
read newname
while [ -f $newname ]
do
echo "this file already exsists, enter file name again"
read newname
done
>$newname
echo "creation of file is successful"
echo "current state of the folder is"
ls
;;
2)
echo "current folder contents are"
ls
echo "enter the name of file to rename"
read oldname
if [ -f $oldname ]
then
echo "enter the name for new file"
read newname
mv $oldname $newname
echo "$oldname has been successfully renamed to $newname"
echo "current state of the folder is "
ls
else
echo "incorrect file name redirecting to earliear menu"
fi
;;
3)
echo "current contents of the folder are as follows"
ls
echo "enter the name of the file to delete"
read delfile
if [ -f $delfile ]
then
rm $delfile
echo "$del file is successfully deleted"
echo "current state of folder is"
ls
else
echo "incorrect file name redirecting to earliear menu"
fi
;;
4)
echo "terminating script"
break
;;
*)
echo "invalid choice, please enter values between 1 and 4 only"
;;
esac
done

Related Posts 

No comments:

Post a Comment