I am attempting to write a sort of menu script which will achieve the below once executed:
The first hurdle I have is getting the password stored as a variable within the menu. I think sshpass will be good for that. I would like to configure a menu like this:
title="Select example"
prompt="Pick an option:"
options=("A" "B" "C")
echo "$title"
PS3="$prompt "
select opt in "${options[@]}" "Quit"; do
case "$REPLY" in
1 ) echo "You picked $opt which is option $REPLY";;
2 ) echo "You picked $opt which is option $REPLY";;
3 ) echo "You picked $opt which is option $REPLY";;
$(( ${#options[@]}+1 )) ) echo "Goodbye!"; break;;
*) echo "Invalid option. Try another one.";continue;;
esac
done
But the menu would ask you for username, local directory of files, i.p. of remote server, remote server directory and then run the scp command it constructed.
Something like this:
password="your password"
username="username"
Ip="<IP>"
sshpass -p "$password" scp /<PATH>/final.txt $username@$Ip:/root/<PATH>
But I'm a bit confused as to how I can put that all of that together so that the menu gathers the required information and then executes the collected input so that it simply constructs an scp command to fire.
Can anyone offer any experience with this to aid me in the construction of the menu script?
Thanks!
If you still need a menu here it is. I modified your script a bit to make it construct/print scp command while entering values one by one.
#!/bin/bash
title="Select example"
prompt="Pick an option:"
declare -A options # turn options to an associative array
options[1]='change user name'
options[2]='change user password'
options[3]='change remote host address'
options[4]='change path to source files'
options[5]='change destination path on remote server'
PS3="$prompt "
menu () { # wrap all this to a function to restart it after edition of an element
echo "$title"
select opt in "${options[@]}" "Quit"; do
case "$REPLY" in
1 ) read -p "${options[1]}: " user;;
2 ) read -sp "${options[2]}: " pass;; # read password with option -s to disable output
3 ) read -p "${options[3]}: " addr;;
4 ) read -p "${options[4]}: " from;;
5 ) read -p "${options[5]}: " dest;;
$(( ${#options[@]}+1 )) ) echo "Goodbye!" ; exit;;
*) echo "Invalid option. Try another one.";;
esac
clear # clear screen to remove inputs
printf "Creating scp command:\n"
# we don't want to show password, print *** if password set and 'password not set' if not
[[ $pass ]] && pass2='***' || pass2='[password not set]'
# text in [] will be printed if var is empty or not set, it's usefull to add default values to vars ${var:-default_value}
printf "sshpass -p $pass2 scp -r ${from:-[source not set]}/* ${user:-[user not set]}@${addr:-[host not set]}:${dest:-[destination not set]}/\n\n"
menu
done
}
clear # clear screen
menu # and start menu function
And here I've got a script with similar (but richer) functionality, please take a look.