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

7
  • 2
    Possible duplicate of How can I create a select menu in a shell script?
    – Melebius
    Jul 15, 2019 at 17:33
  • Try using backticks for the options variable, remove the '-d' and replace the '-l' with '-1' (the number one). Like this: options=ls -1 $download Jul 15, 2019 at 17:49
  • It says now "ls: Can not access '~ / Downloads': file or directory not found"
    – Lockna
    Jul 15, 2019 at 18:01
  • Why are there space in your '~ / Downloads' ?
    – Rinzwind
    Jul 15, 2019 at 18:40
  • There aren't any spaces
    – Lockna
    Jul 15, 2019 at 18:45

3 Answers 3

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
4
  • Writing my own answer and learning from yours :) I think you can shorten by one line with options=( ~/"Downloads"/* ) and deleting line before. Jul 15, 2019 at 23:17
  • @WinEunuuchs2Unix sure you can - I was just trying to preserve as much as possible of the OP's code structure Jul 15, 2019 at 23:20
  • ahh good point, I'll be preserving none in my answer :) Jul 15, 2019 at 23:21
  • Your answer worked. Thank you very much^^
    – Lockna
    Jul 22, 2019 at 10:35
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.