I have this script:
#!/bin/bash
menu()
{
while true; do
opt=$(whiptail \
--title "Select an item" \
--menu "" 20 70 10 \
"1 :" "Apple" \
"2 :" "Banana" \
"3 :" "Cherry" \
"4 :" "Pear" \
3>&1 1>&2 2>&3)
rc=$?
echo "rc=$rc opt=$opt"
if [ $rc -eq 255 ]; then # ESC
echo "ESC"
return
elif [ $rc -eq 0 ]; then # Select/Enter
case "$opt" in
1\ *) echo "You like apples"; return ;;
2\ *) echo "You go for bananas"; return ;;
3\ *) echo "I like cherries too"; return ;;
4\ *) echo "Pears are delicious"; return ;;
*) echo "This is an invalid choice"; return ;;
esac
elif [ $rc -eq 1 ]; then # Cancel
echo "Cancel"
return
fi
done
}
menu
When I press ESC button, the output is as expected:
rc=255 opt=
ESC
Now, by making opt a local variable, the behaviour is different:
...
local opt=$(whiptail \
...
Output:
rc=0 opt=
This is an invalid choice
Can someone explain this?
$? is getting the return code of the local command. Try making the local command and the assignment separate statements:
local opt
opt=$(whiptail ...
I found this wonderful tool to check a bash script for possible bugs...
$ shellcheck myscript
Line 6:
local opt=$(whiptail \
^-- SC2155: Declare and assign separately to avoid masking return values.
$
Related
I just started to learn bash and create a GUI with Dialog, but I'm having a problem with my program, any help will be appreciate it. Thanks
I want to create a program which will display a dialog which will ls only directories from the current folder:
display_folders()
{
while true; do
let count=0 #define counting variable
w=() #define working array
while read -r line; do #process file by file
let count=$count+1
w+=("$line" "$line")
done < <(ls -d */)
file=$(dialog --title "List directory" --cancel-label "Exit" --no-tags --menu "Please choose one folder: " 10 40 0 "${w[#]}" 3>&2>
#clear
exit_status=$?
echo $exit_status
case $exit_status in
1) echo "Program terminated"
exit ;;
255) echo "Program aborted"
exit 1 ;;
esac
echo "this is $file"
case "$file" in
*)
cd $file
display_result "$file" ;;
esac
done
}
After selecting the specified dir (for example ANIMALS) I want to cd into it and make some actions (the code is just for the 1 selection)
display_result()
{
while true; do
selection=$(dialog --title "folder" \
--cancel-label "Exit" \
--menu "Choose an action: " 10 40 0 \
"1" "List details about files" \
"2" "Search for word" \
"3" "Generate CSV" \
"4" "More info CSV" \
"5" "Search file" \
3>&2 2>&1 1>&3)
exit_status=$?
case $exit_status in
1) break ;;
255) echo "Program aborted"
exit 1 ;;
esac
case $selection in
1 )
result=$(ls -lt)
display_file_details ;;
esac
done
}
display_file_details()
{
dialog --title "file details" --no-collapse --msgbox "$result" 0 0
}
The problem is, in the selected folder (ANIMALS) I have another folder too (for example OTHERS), when I am ls all from the folder ANIMALS it will display me everything (which is good), but after I exit from the display --msgbox will display me another --menu only with OTHERS folder, and the display_result for it, if I exit from this too, the program will exit with 1 code.
What I want is to cd into ANIMALS, which is the dir from current folder, then list the options (1,2,3,4,5), and after exit from the options display I want to take me back to my current folder with ANIMALS in it.
You don't need this 'while true; do' loops. Create first dialog:
dialog1(){
list=( */ )
folder=$( dialog --title "List directory" --cancel-label "Exit" \
--no-items --menu "Please choose one folder: " \
--output-fd 1 10 40 0 ${list[#]///} )
echo $folder
dialog2
}
And the second like this:
dialog2(){
# another dialog here
# some code here
dialog1 # run first dialog again
}
And start first dialog:
dialog1
p.s. check out my projects sshto and kube-dialog created via dialog.
Thank's #Ivan but this is not what I wanted.
I solved it by going back with a dir in the same case :
display_file_details()
{
dialog --title "file details" --no-collapse --msgbox "$1" 0 0
}
dialog1(){
list=( */ )
folder=$( dialog --title "List directory" --cancel-label "Exit" \
--no-items --menu "Please choose one folder: " \
--output-fd 1 10 40 0 ${list[#]///} )
exit_status=$?
echo "$exit_status dialog1"
case $exit_status in
1 | 255)
return 0
;;
*)
diag2_return=255
while [ "$diag2_return" -ne "0" ]; do
dialog2 $folder
diag2_return=$?
done
esac
return 1
}
dialog2(){
echo "hereeeeeeeeee $1"
selection=$(dialog --title "folder" --cancel-label "Exit" \
--menu "Choose an action: " --output-fd 1 10 40 0 \
"1" "List details about files" \
"2" "Search for word" \
"3" "Generate CSV" \
"4" "More info CSV" \
"5" "Search file" )
exit_status=$?
echo "$exit_status dialog2"
case $exit_status in
1 | 255)
return 0
;;
esac
case $selection in
1 )
cd "$1"
result=$(ls -lt)
display_file_details "$result"
cd ..
;;
esac
return 1
}
diag_return=255
while [ "$diag_return" -ne "0" ]; do
dialog1
diag_return=$?
done
I have a script in bash as such:
#!/usr/bin/env bash
set -e
if [[ "$#" == 0 ]]; then
printhelp
exit 1
fi
# process options
while [[ "$1" != "" ]]; do
case "$1" in
-n | --name)
shift
_NAME="$1"
;;
-i | --id)
shift
_ID="$1"
;;
-h | --help)
printhelp
exit 1
;;
*)
printhelp
exit 1
;;
esac
shift
done
This works fine, but I want to add some "actions" that will take the above params. Eg. usage will be:
./run.sh create --name foo --id 1234
./run.sh delete --id 1234
I am not able to figure out the right syntax, and I am unable to phrase this requirement into appropriate words to be able to search.
For sub-command you can handle it this way:
function main(){
if (( ${#} == 0 )); then
main_help 0;
fi
case ${1} in
help | version | encrypt | decrypt )
$1 "${#:2}";
;;
* )
echo "unknown command: $1";
main_help 1;
exit 1;
;;
esac
}
main "$#";
Then wrap each sub-command is a function. And inside each function you will have isolated options and parsing it separately.
For example:
function decrypt(){
if [[ ${#} == 0 ]]; then
decrypt_help;
fi
local __filename='';
local __salt='';
local __anchor=false;
local error_message='';
while [ ${#} -gt 0 ]; do
error_message="Error: a value is needed for '$1'";
case $1 in
-f | --file )
__filename=${2:?$error_message}
shift 2;
;;
-s | --salt )
__salt=${2:?$error_message}
shift 2;
;;
-a | --anchor )
__anchor=${2:?$error_message}
shift 2;
;;
* )
echo "unknown option $1";
break;
;;
esac
done
echo filename: ${__filename:-empty};
echo salt: ${__salt:-empty};
echo anchor: $__anchor;
exit 0;
}
Here is a full version bash-CLI-template I have used in my projects
demo ;)
Sounds like you want something like:
create() {
# actiony stuff here
}
ACTION=$1 ; shift
# put all your argument parsing here
$ACTION # call
However, since different actions probably have different arguments, I'd probably do it differently...
create() {
# argument parsing for create
# then do your create stuff
}
ACTION=$1 ; shift
$ACTION "$#"
This will pass all your arguments to your subfunction, which can then parse its own arguments.
UPDATE:::
In the comments, I've got an answer. Thank you!
:::
I have this sample, I want it to run multiple commands and give an output but It runs only the first command 'terraform init'. How can tell it to run all of the commands with the if statement that checks if file exists or not?
file = ./launchpad.yaml
function run_with_existing_config
{
option=0
until [ "$option" = "2" ]; do
printf "\n ${menu} 1.) ${normal} Run your Cluster \n"
printf "\n ${menu} 2.) ${normal} Return to the Main Menu \n"
read -p 'Enter choice: ' option
echo ""
case $option in
1 ) run_cluster ;;
2 ) main_menu ;;
3 ) break ;;
* ) tput setf 2;printf "\n ${menu} Please enter ${number} 1 ${menu} or ${number} 2 \n"; tput setf 2;
esac
done
}
function run_cluster
{
terraform init
#terraform apply -auto-approve
if [ ! -e "$file" ]; then
continue
else
mv -i ./$file ./$file.$(date +"%Y-%m-%d_%H-%M").yaml
fi
terraform output --raw mke_cluster > ./$file
cat ./$file
#launchpad apply
exit 0
}
Problem:
I need to make this bash script to choose based on the user inputs. Example how can i add choices? such that user just select from 1 to 3 and that is set in variable CLUSTER_NAME.
choices are test.com, try.com and me.com
Script
#!/bin/bash
sops_ops() {
sops --version
if [ "$?" -eq "0" ]; then
echo "proceed sops ops"
else
echo "check sops binary"
fi
read -p 'Enter cluster_NAME: = ' CLUSTER_NAME
test_environment="test.com"
test1_environment="test1.com"
test2_environment="test2.com"
case "${$CLUSTER_NAME}" in
prod.$test_environment) ;;
dev.$test1_environment) ;;
stage.$test2_environment) ;;
test.$test_environment) ;;
*) echo "Invalid option: ${CLUSTER_NAME}" 1>&2 && exit 1 ;;
if [ $CLUSTER_NAME = test.$test_env ];then
printf "got cluster $CLUSTER_NAME"
elif [ $CLUSTER_NAME = "test.test.com" ];then
printf "got dev cluster $CLUSTER_NAME"
echo "not found cluster"
else
echo "Environment not available"
fi
}
sops_ops
Question:
How do I do that?
Any help is appreciated!
I have these two scripts, configScript.shand genScript.sh. The first one works just the way I want it to work. It adds the correct values into options.sh and echo the right message. However, I want genScript.sh to accept the current argument in options.sh and output the correct echo. As it is now when I run genScript.sh it returns null and I can't figure out why.
#!/bin/bash -x
#configScript.sh
func()
{
echo "
Choose
1 - Option 1
2 - Option 2
"
echo -n " Enter selection: "
read select
case $select in
1 )
echo " Option 1 chosen"
. ./genScript.sh one
cat << EOF >options.sh
OPTION=$OPTION
EOF
;;
2 )
echo " Option 2 chosen"
. ./genScript.sh two
cat << EOF >options.sh
OPTION=$OPTION
EOF
;;
esac
}
func
#!/bin/bash -x
#genScript.sh
. options.sh
OPTION=$1
func2()
{
if [ "$OPTION" == one ] ; then
echo "Option one"
elif [ "$OPTION" == two ] ; then
echo "Option two"
else
echo "null"
fi
}
func2
I managed to get genScript.sh to work the way I want by removing OPTION=$1. When I do that genScript.sh will accept the value inside options.sh and will output the right echo . BUT when I remove OPTION=$1 configScript.sh stops working as it should, it doesn't update options.sh with a new value anymore.
The problem is with the way you want genScript to be called. I think you want to run genScript with command line argument and as-well as with sourcing from options.sh.
Below changes to genScript.sh would serve the purpose. It gives preference to command line when both command line and options.sh have values.
#!/bin/bash -x
#genScript.sh
OPTION=""
. options.sh
[ "$1" ] && OPTION=$1
func2()
{
if [ "$OPTION" == one ] ; then
echo "Option one"
elif [ "$OPTION" == two ] ; then
echo "Option two"
else
echo "null"
fi
}
func2
Just put quotes around "one" and "two" in the second script and in the first script where it generates options.sh and added default value $OPTION to the OPTION var in the second script so now it works.
#!/bin/bash -x
#configScript.sh
func()
{
echo "
Choose
1 - Option 1
2 - Option 2
"
echo -n " Enter selection: "
read select
case $select in
1 )
echo " Option 1 chosen"
. ./genScript.sh one
cat << EOF >options.sh
OPTION="$OPTION"
EOF
;;
2 )
echo " Option 2 chosen"
. ./genScript.sh two
cat << EOF >options.sh
OPTION="$OPTION"
EOF
;;
esac
}
func
#!/bin/bash -x
#genScript.sh
. options.sh
OPTION=${1-$OPTION}
func2()
{
if [ "$OPTION" == "one" ] ; then
echo "Option one"
elif [ "$OPTION" == "two" ] ; then
echo "Option two"
else
echo "null"
fi
}
func2
That is one of the most irritating problems. I don't know if you are using editor with syntax highlighting but you better be so you run easily over this type of issues.