Advertisement
Guest User

Untitled

a guest
May 26th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3.  
  4.  
  5. function choosefile {
  6. # ask filename
  7. echo "Choose a file name to work with and press enter"
  8. read file
  9. echo " "
  10. echo "We will work with $file "
  11. echo " "
  12. }
  13.  
  14. function createfile {
  15. echo "create $file"
  16. touch $file
  17. }
  18.  
  19. function deletefile {
  20. echo "delete $file"
  21. rm $file
  22. }
  23.  
  24. function addpermition {
  25. echo "Enter R to give read permition, W for write, X to execute ?"
  26. select i in R W X; do
  27. if [ "$i" = "R" ]; then
  28. echo "give read permition on $file"
  29. break
  30. elif [ "$i" = "W" ]; then
  31. echo "give write permition on $file"
  32. break
  33. elif [ "$i" = "X" ]; then
  34. echo "give execute permition on $file"
  35. break
  36. else
  37. echo "Bad choice"
  38. fi
  39. done
  40. echo "chown $user $file"
  41. }
  42.  
  43. function edit {
  44. nano $file
  45. }
  46.  
  47. function 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 backups of $file
  55. $backup="Please select a backup to restore:"
  56. options=( $(find . -name "*$file-backup*" -maxdepth 1 -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.  
  77. function displaysize {
  78. echo "Size of $file: "
  79. ls -s $file
  80. }
  81.  
  82. function findfile {
  83. echo "Look for $file on current directory"
  84. find . -name "$file"
  85. }
  86.  
  87.  
  88. choosefile
  89. PS3="Make a choice: (press enter to display menu) "
  90.  
  91. 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"
  92. do
  93. for var in $REPLY; do
  94. echo "Your choice: $var : $item"
  95. case $var in
  96. 1)
  97. # call createfile function
  98. createfile
  99. ;;
  100. 2)
  101. # call deletefile function
  102. deletefile
  103. ;;
  104. 3)
  105. # call deletefile function
  106. edit
  107. ;;
  108. 4)
  109. # call deletefile function
  110. backup
  111. ;;
  112. 5)
  113. # call deletefile function
  114. restore
  115. ;;
  116. 6)
  117. # call deletefile function
  118. displaysize
  119. ;;
  120. 7)
  121. # call deletefile function
  122. addpermition
  123. ;;
  124. 8)
  125. # call deletefile function
  126. findfile
  127. ;;
  128. 9)
  129. echo "End of script"
  130. exit 0
  131. ;;
  132. 10)
  133. choosefile
  134. ;;
  135. *)
  136. echo "Incorrect selection"
  137. ;;
  138. esac
  139. done
  140. done
  141.  
  142.  
  143. #création, manipulation, droits d'accès, sauvegarde, tri, recherche
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement