0

I want to program a script, which helps me installing thinks. ( I program it for bash exercises) I want to have a menu with "select option in options"

#!/bin/bash
download="~/Downloads/"
options='ls -l -d $download'
select option in $options; do
    if [ "Test" = $option ]; then
        clear
        exit
    else 
        echo hello
    fi
done

The script should give every file in Download to select. And when its a tar.gz file it should install it in opt. But I do not know how to get the selection menu

Lockna
  • 113
  • 5

3 Answers3

1

Here's a minimal working example. Note the following:

  1. tilde (~) doesn't expand inside double quotes, so needs to be outside. Consider replacing download=~/"Downloads" by download="$HOME/Downloads"

  2. don't use ls to generat a list of filename - it will break if any of the filenames has whitespace. Use a shell glob *, and put the results in an array

So:

#!/bin/bash

download=~/"Downloads"

options=( "$download"/* )

select option in "${options[@]}" "quit"; do
  case $option in
    *.tar.gz)
      echo "install some stuff"
      ;;    
    "quit")
      break ;;
    *)
      echo "You chose $option" ;;
  esac
done
steeldriver
  • 136,215
  • 21
  • 243
  • 336
1

You said you were learning bash so I thought you might appreciate a different kind of answer using zenity which is a popular GUI for bash built into Ubuntu.

Zenity Dialog Box

Here's what the zenity file selection dialog box looks like:

Downloads.sh screenshot.png

Calling the script

Here is how to call the script and what it outputs to your terminal when you select an item on the list:

$ Downloads.sh
Option: /home/rick/Downloads/nvhda.tar.xz

Instead of displaying "Option: " your version of the script will be running a tar command.

Downloads.sh bash script

#!/bin/bash

NAME: Downloads.sh

PATH: ~/askubuntu

DESC: Select Download file

DATE: July 15, 2019.

NOTE: For: https://askubuntu.com/questions/1158485/how-to-get-ls-in-select-in

Array=( ~/"Downloads"/* ) while true; do Option=$(zenity --list --title="Downloads.sh" --text="Select file"
--ok-label "TAR process" --cancel-label "Exit"
--width=800 --height=600 --column="Filename" "${Array[@]}" 2>/dev/null) [[ "$Option" == "" ]] && break echo "Option: $Option" done

0

The select command will try to display every field separated by spaces, tabs, and newline characters. So I don't think your sample will do what you want. You must not use "ls -l" for long listing or the permissions and ownerships and file sizes will all be fields displayed in the select statement results.

Not sure if this is exactly what you were looking for, but it finds all files (no directories or symbolic links) and decides whether to process it as a PostgreSQL backup or just a regular file. You could always adapt it to look for whatever it is you hope to find in the downloads directory.

You could look at changing the if statement to a case and try to find specific extensions. So you find the .tar or .gz or .cpio, and so on. Just figure out how to strip off the extension.

#!/bin/bash
download="/tempfs"
cd $download
options=$(ls -l |grep -v "^d" |awk '{print $9}')
select option in $options
do
if test "$(echo $option|grep ".dmp$")" = ""
then
  echo "Process as normal file: ${option}"
  exit 0
else
  echo "Process as pg_dump file: ${option}"
  exit 0
fi
done
S. Nixon
  • 402