Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- function choosefile {
- # ask filename
- echo "Choose a file name to work with and press enter"
- read file
- echo " "
- echo "We will work with $file "
- echo " "
- }
- function createfile {
- # create file
- echo "create $file"
- touch $file
- }
- function deletefile {
- echo "delete $file"
- rm $file # delete file
- }
- function addpermition { # give permisions to file (R,W,X)
- echo "Enter R to give read permition, W for write, X to execute ?"
- select i in R W X; do
- if [ "$i" = "R" ]; then
- echo "give read permition on $file"
- chmod +r $file
- break
- elif [ "$i" = "W" ]; then
- echo "give write permition on $file"
- chmod +w $file
- break
- elif [ "$i" = "X" ]; then
- echo "give execute permition on $file"
- chmod +x $file
- break
- else
- echo "Bad choice"
- fi
- done
- }
- function edit { # edit file
- nano $file
- }
- function backup { # create backup for the file and add the date of the backup
- date=$(date '+%Y-%m-%d-%H_%M_%S')
- echo "Create backup of $file to $file-backup-$date"
- cp -p $file $file-backup-$date
- }
- function restore {
- # List of all backups of the files and display them on the menu
- echo "Please select a backup to restore:"
- options=( $(find . -maxdepth 1 -name "*$file-backup*" -print0 | xargs -0) )
- # create menu to permit to select file to restore
- PS3="$backup "
- select opt in "${options[@]}" "Quit" ; do
- if (( REPLY == 1 + ${#options[@]} )) ; then
- exit
- elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
- echo "You choose $opt which is file $REPLY"
- break
- else
- echo "Bad selection"
- fi
- done
- # copy backup
- cp -v $opt $file
- echo "$opt restored on $file"
- }
- # Dislay size of the file
- function displaysize {
- echo "Size of $file: "
- ls -s $file
- }
- function renamefile {
- echo "Choose a new name for $file"
- read newfile
- mv $file $newfile
- }
- # Call function choosefile to select a file name
- Choosefile
- # Display Menu for the Program
- PS3="Make a choice: (press enter to display menu) "
- select item in "- Create the file" "- Delete the file" "- Edit the file" "- Create a backup " "- Restore a backup " "- display size " "- Add permition " "- Find file " "- Exit script " "- Change file"
- do
- for var in $REPLY; do
- echo "Your choice: $var : $item"
- case $var in
- 1)
- # call createfile function
- createfile
- ;;
- 2)
- # call deletefile function
- deletefile
- ;;
- 3)
- edit
- ;;
- 4)
- backup
- ;;
- 5)
- restore
- ;;
- 6)
- displaysize
- ;;
- 7)
- addpermition
- ;;
- 8)
- renamefile
- ;;
- 9)
- echo "End of script"
- exit 0
- ;;
- 10)
- choosefile
- ;;
- *)
- echo "Incorrect selection"
- ;;
- esac
- done
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement