Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. #! /bin/bash
  2. #This bash compresses and decompresses files
  3. act=-1 #A variable used to transfer which action has been chosen
  4. action1="Compress"
  5. action2="Decompress" #action1 & 2 both used for certain echoes
  6.  
  7. function menu { #The menu
  8. clear
  9. local title="-------Menu-------"
  10. local prompt="Please choose an action:"
  11. local options=("$action1" "$action2" "Quit")
  12.  
  13. echo "$title"
  14. PS3="$prompt" #Changes the default '#?' that the select command uses to $prompt.
  15. select option in "${options[@]}";do
  16.  
  17. case $option in
  18.  
  19. ${options[0]})
  20. echo "You chose $option"
  21. act=0 #Changes act to 0 , to be used later
  22. break
  23. ;;
  24.  
  25. ${options[1]})
  26. echo "You chose $option"
  27. act=1 #Changes act to 1 , to be used later
  28. break;
  29. ;;
  30. ${options[2]})
  31. echo "You chose $option"
  32. exit
  33. ;;
  34. *)
  35. echo "Invalid option please choose between 1-3"
  36. ;;
  37. esac
  38. break
  39. done
  40. }
  41.  
  42. function ynPrompt { #a y/n prompt to go back to the main menu or exit
  43. while true #helps to loop the y/n prompt in case of wrong input ex. a343
  44. do
  45.  
  46. read -r -p "Do you want to go back to the menu? [y/N] " response
  47. case $response in
  48.  
  49. [yY][eE][sS]|[yY]) #accepts yes or y and ignores casing ex. yEs is accepted.
  50. continue 2 #continues the outer control loop
  51. ;;
  52. [nN][oO]|[nN]) #accepts no or n and ignores casing ex. nO is accepted.
  53. exit #exits the script
  54. ;;
  55. *)
  56. continue #shows the message again
  57. ;;
  58. esac
  59. break
  60. done
  61. }
  62.  
  63. function main { #Performs the selected action
  64. if [ $act -eq 0 ]; then
  65.  
  66. if zip -r ${path}.zip ${path}
  67. then echo Compression successful
  68. echo $? #prints 0
  69. else echo $?
  70. fi
  71.  
  72.  
  73. #echo "$action1"
  74. #zip -r ${path}.zip ${path}
  75.  
  76. elif [ $act -eq 1 ]; then
  77.  
  78. if unzip ${path}
  79. then echo Decompression successful
  80. echo ${path}
  81. echo $? #prints 0
  82. else echo $?
  83. fi
  84.  
  85. #echo "$action2"
  86. #unzip ${path}
  87.  
  88.  
  89. else
  90. echo "$error"
  91. fi
  92.  
  93. }
  94.  
  95.  
  96.  
  97. #~~~~~~~~~~~~ Script start ~~~~~~~~~~~~~~~~
  98. while true #outer control loop
  99. do
  100. menu #call menu
  101. cd /home
  102. path=$(zenity --file-selection) #Stores the path of the file into path variable through zenity dialog
  103. #path can only be .zip if i had --file filter *.zip
  104.  
  105. if [ -z $path ]; then #checks length of $path , returns true if length = 0
  106. ynPrompt #call ynprompt
  107. else
  108. main #call main
  109. fi
  110.  
  111. break
  112. done
  113.  
  114. compress_file ()
  115. {
  116. local dir file
  117. test -f "$1" || return 2
  118. dir="$(readlink -f "$1")"
  119. file="${dir##*/}"
  120. dir="${dir%/*}"
  121. cd "$dir"
  122. # check whether target file exists:
  123. # test -f "$file".zip && : whatever
  124. echo zip "$file".zip "$file"
  125. }
  126.  
  127. compress_file /path/to/file
  128.  
  129. zipfile_dialog ()
  130. {
  131. local file startdir="/home/ubuntu" tmpdirname=.zipscript.$$
  132. cd "$startdir" || return 2
  133. test -d "$tmpdirname" && { rm -r "$tmpdirname" || return 2; }
  134. mkdir -p "$tmpdirname" || return 2
  135. for file in *.zip; do
  136. cd "$tmpdirname"
  137. ln -s ../"$file"
  138. cd ..
  139. done
  140. ls "$tmpdirname"
  141. # call zenity here
  142. rm -r "$tmpdirname"
  143. }
  144.  
  145. zipfile_dialog
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement