bash script, command "select" hold scrolling

589 Views Asked by At

I have this bash script I would like to pause so I can read what is echoed. I use this on a mobile phone so the screen is very small. The continent output is in 2 colums by default, can I do this with the city output aswell

I tried putting " | more " before && but it didn't work.

normaly I put everything in functions() but finding the select command here on stackoverflow.com made it much faster.

#!/bin/bash
PS3=" Enter a Number "
dir=/usr/share/zoneinfo
echo
echo " Please select your Continent "
echo
select continent in $dir/* ; do
    test -n "$continent" && break;
    echo ">>> Invalid Selection";
done
selected_cont=($(echo "$continent" | grep -Eo '[^/]+/?$' | cut -d'/' -f2 ))
echo
echo " Selected $selected_cont as your Continent "
echo
echo  " Please select nearest city "
select city in $continent/* ; do
    test -n "$city" && break;
    echo ">>> Invalid Selection";
done
echo
selected_city=($(echo "$city" | grep -Eo '[^/]+/?$' | cut -d'/' -f2 ))
timezone="$selected_cont/$selected_city"
echo
echo
echo " $timezone is selected as your timezone "
#echo "show Continent: $continent"
#echo "show City: $city"
sleep 2
#echo export TZ=$city >> $HOME/.bashrc
#sudo ln -sf $city /etc/localtime
1

There are 1 best solutions below

2
On

This was my first working try

#!/bin/bash

dir=("/usr/share/zoneinfo/")

continent=$(dialog --stdout --title "Select Continent" --dselect $dir 0 0 --no-mouse --aspect 9 --ascii-lines) 

city=$(dialog --stdout --title "Please choose a file" --fselect $continent/ 14 48)

#echo ${continent} selected 
#echo ${city} selected

selected_cont=($(echo ${continent} | grep -Eo '[^/]+/?$' | cut -d'/' -f2))
# Strip full path to last directory to get the continent. use it with $selected_continent

selected_city=($(echo ${city} | grep -Eo '[^/]+/?$' | cut -d'/' -f2))
# Strip full path to last directory to get the nearest city. use it with $selected_city

#    echo "export TZ=(${continent/${city})" >> $HOME/.bashrc 
#    sudo ln -sf ${city} /etc/localtime

echo $selected_cont selected as continent
echo selected $selected_city as nearest city
echo "set TZ in bashrc TZ=$selected_cont/$selected_city "
echo "link ${city} to /etc/localtime "

########EOF###########

This is my second try with directory and file check

#!/bin/bash

start() {
if [[ -z $selected_cont ]]; then 

    continent=($(dialog --stdout --title "Select Continent" --dselect $dir 0 0 --no-mouse --aspect 9 --ascii-lines)); 
    selected_cont=($(echo ${continent} | grep -Eo '[^/]+/?$' | cut -d'/' -f2));
    if [[ -d $dir$selected_cont ]]; then 
        echo $dir$selected_cont
        start
    else
        selected_cont=
        start
    fi
elif [[ -z $selected_city ]]; then

    city=($(dialog --stdout --title "Please choose a file" --fselect $continent/ 14 48));
    selected_city=($(echo ${city} | grep -Eo '[^/]+/?$' | cut -d'/' -f2));
    if [[ -f ${city} ]]; then
        echo $selected_city
        start
    else
        selected_city=
        start
    fi
else
#    echo "export TZ=(${continent/${city})" >> $HOME/.bashrc 
#    sudo ln -sf ${city} /etc/localtime

echo $selected_cont selected as continent
echo selected $selected_city as nearest city
echo "set TZ in bashrc TZ=$selected_cont/$selected_city "
echo "link ${city} to /etc/localtime "
fi
}

start
######EOF######

This 2nd one is preferred but it errors on line 6 continent= Error: Expected a box option. The script continuous as wanted and the result is as wanted.

Can anyone clarify the error as I use the same variable in script one without errors?

I've updated to output to a temp file no errors in this script

#!/bin/bash
dir=("/usr/share/zoneinfo/")

temp=/tmp/test.$$

start() {
if [[ -z $selected_cont ]]; then 

    continent=($(dialog --stdout --title "Select Continent" --dselect $dir 0 0 --no-mouse --aspect 9 --ascii-lines 2>&1 > $temp));
    selected_cont=($(cat $temp | grep -Eo '[^/]+/?$' ));
    echo 
    if [[ -d $dir$selected_cont ]]; then 
        start
    else
        selected_cont=
        start
    fi
elif [[ -z $selected_city ]]; then

    city=($(dialog --stdout --title "Please choose a file" --fselect $dir$selected_cont/ 0 0 2>&1 > $temp));
    selected_city=($(cat $temp | grep -Eo '[^/]+/?$' ));
    if [[ -f $(cat $temp) ]]; then
        start
    else
        selected_city=
        start
    fi
else
#    echo "export TZ=(${continent/${city})" >> $HOME/.bashrc 
#    sudo ln -sf $(cat $temp) /etc/localtime

echo $selected_cont selected as continent
echo selected $selected_city as nearest city
echo "set TZ in bashrc TZ=$selected_cont/$selected_city "
echo link $(cat $temp) to /etc/localtime
fi
}

start