Advertisement
tolikpunkoff

asklist-select (simple menu with select operator)

May 11th, 2019
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.84 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. FOUNDLST=""
  4. DIR1="/home/smallwolfie/test/archives"
  5. TARGZ=""
  6.  
  7. create_list() #$1 - dir, $2 - file mask
  8. {
  9.     FOUNDLST=""
  10.     for FLE in $(find $1 -maxdepth 1 -iname $2); do
  11.     if [ -n "$FLE" ]; then
  12.         FOUNDLST="$FOUNDLST"`basename $FLE`"\n"
  13.     fi
  14.     done
  15. }
  16.  
  17. ask_list() #$1 - list #$2 - header
  18. {
  19.     LIST_BUF=$1
  20.     LIST_BUF="$LIST_BUF""Cancel"
  21.     LIST_BUF=`echo -e "$LIST_BUF"|sed 's/\n/ /'`
  22.    
  23.     PS3=$2
  24.    
  25.     echo
  26.     select LIST_RET in $LIST_BUF; do
  27.     if [ -n "$LIST_RET" ];then
  28.         break
  29.     fi
  30.     done
  31.    
  32. }
  33.  
  34. create_list $DIR1 "*.tar.gz"
  35.  
  36. if [ -z "$FOUNDLST" ]; then
  37.     echo "$DIR1 *.tar.gz not found"
  38.     exit
  39. else
  40.     TARGZ="$FOUNDLST"
  41. fi
  42.  
  43. ask_list "$TARGZ" "Select file: "
  44.  
  45. if [[ "$LIST_RET" == "Cancel" ]]; then
  46.     echo "Cancelled by user!"
  47.     exit
  48. fi
  49.  
  50. echo "User select: $LIST_RET"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement