Advertisement
Guest User

Untitled

a guest
May 26th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.05 KB | None | 0 0
  1. #!/bin/bash
  2. function choosefile {
  3.         # ask filename
  4.         echo "Choose a file name to work with and press enter"
  5.         read file
  6.         echo " "
  7.         echo "We will work with $file "
  8.         echo " "
  9. }
  10.  
  11. function createfile {
  12.     # create file
  13.         echo "create $file"
  14.         touch $file
  15. }
  16.  
  17. function deletefile {  
  18.         echo "delete $file"
  19.         rm $file    # delete file
  20. }
  21.  
  22. function addpermition {   # give permisions to file (R,W,X)
  23.         echo "Enter R to give read permition, W for write, X to execute ?"
  24.         select i in R W X; do
  25.             if [ "$i" = "R" ]; then
  26.                     echo "give read permition on $file"
  27.                     chmod +r $file
  28.             break
  29.             elif [ "$i" = "W" ]; then
  30.                     echo "give write permition on $file"
  31.             chmod +w $file
  32.                     break
  33.             elif [ "$i" = "X" ]; then
  34.                     echo "give execute permition on $file"
  35.             chmod +x $file
  36.                     break
  37.             else
  38.                     echo "Bad choice"
  39.             fi
  40.         done
  41.        }
  42.  
  43. function edit {   # edit file
  44.         nano $file
  45. }
  46.  
  47. function backup {   # create backup for the file and add the date of the backup
  48.         date=$(date '+%Y-%m-%d-%H_%M_%S')
  49.         echo "Create backup of $file to $file-backup-$date"
  50.         cp -p $file $file-backup-$date
  51. }
  52.  
  53. function restore {
  54.         # List of all backups of the files and display them on the menu
  55.         echo "Please select a backup to restore:"
  56.         options=( $(find . -maxdepth 1 -name "*$file-backup*" -print0 | xargs -0) )
  57.         # create menu to permit to select file to restore
  58.         PS3="$backup "
  59.         select opt in "${options[@]}" "Quit" ; do
  60.             if (( REPLY == 1 + ${#options[@]} )) ; then
  61.                 exit
  62.  
  63.             elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
  64.                 echo  "You choose $opt which is file $REPLY"
  65.                 break
  66.  
  67.             else
  68.                 echo "Bad selection"
  69.             fi
  70.         done
  71.         # copy backup
  72.         cp -v $opt $file
  73.         echo "$opt restored on $file"
  74.  
  75. }
  76. # Dislay size of the file
  77. function displaysize {
  78.         echo "Size of $file: "
  79.         ls -s $file
  80. }
  81.  
  82. function renamefile {
  83.         echo "Choose a new name for $file"
  84.  read newfile
  85.         mv $file $newfile
  86. }
  87.  
  88.  # Call function choosefile to select a file name
  89.  
  90.  
  91. Choosefile
  92. # Display Menu for the Program
  93. PS3="Make a choice: (press enter to display menu) "
  94.  
  95. 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"
  96. do
  97.     for var in $REPLY; do
  98.         echo "Your choice: $var : $item"
  99.         case $var in
  100.                 1)
  101.                         # call createfile function
  102.                         createfile
  103.                         ;;
  104.                 2)
  105.                         # call deletefile function
  106.                         deletefile
  107.                         ;;
  108.                 3)
  109.                      
  110.                         edit
  111.                         ;;
  112.                 4)
  113.                        
  114.                         backup
  115.                         ;;
  116.                 5)
  117.                        
  118.                         restore
  119.                         ;;
  120.                 6)
  121.                        
  122.                         displaysize
  123.                         ;;
  124.                 7)
  125.                        
  126.                         addpermition
  127.                         ;;
  128.                 8)
  129.                        
  130.                         renamefile
  131.                         ;;
  132.                 9)
  133.                         echo "End of script"
  134.                         exit 0
  135.                         ;;
  136.                 10)
  137.                         choosefile
  138.                         ;;
  139.                 *)
  140.                         echo "Incorrect selection"
  141.                         ;;
  142.         esac
  143.     done
  144. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement