I'm thinking about creating a bash script where multiple options can be specified and at the end define the variables according to the chosen options or execute certain orders when receiving the different options. An example is worth more than a thousand words:
[X] Copy only (1) - Options typed by the user [ ] Move only (2) [X] Checksum (3) - Options typed by the user [ ] Reset permission (4) [ ] Exit (5) Select choice:
I have found some options but I do not know how to make them do a certain function because I do not understand how the code works.
Update:
Code functional:
#!/bin/bash #Contributing code by Sergiy Kolodyazhnyy and adaptation for MarianoM. resize -s 40 90 > /dev/null #Change window size. option=$(dialog --clear --backtitle "Synchronization..." --title "Synchronize" \ --checklist "Select the synchronization options:" 20 50 10 \ checksum "Compare the content" off \ detail "Show more information" off \ directory "Synchronize folders" on \ recursive "Include subfolders" off 2>&1 > /dev/tty) for i in $option; do #Set parameter of the menu options. case $i in checksum) c="-c" ;; detail) v="-v" ;; directory) d="-d" ;; recursive) r="-r" ;; esac done if [ -z $option ]; then #Check if the variable is empty. If it is empty, it means that the user has not chosen an option. clear echo echo "Error: No option has been selected or dialog not installed. The program can not continue." echo else clear source=$(dialog --clear --backtitle "Please select source..." --title "Source:" --fselect "" 20 50 2>&1 > /dev/tty) if [ -z $source ]; then clear echo echo "Error program. Source not selected, try again!" echo exit fi clear destination=$(dialog --clear --backtitle "Please select destination..." --title "Destination:" --fselect "" 20 50 2>&1 > /dev/tty) if [ -z $destination ]; then clear echo echo "Error program. Destination not selected, try again!" echo exit fi clear rsync "$c" "$v" "$d" "$r" "$source" "$destination" echo fi exit
select
statement, so in such regard it's duplicate. However since you want to provide multiple options at the same time, it's not quite a duplicate. What can be done, however, is to allowselect
statement to loop through multiple options and execute each one after the other. I would also caution against providing multiple options at the same time, since if you try to provide options like2,1,3
- well the file will be moved, and next two options will fail due to missing file.c=$(echo "-c")
can be simply writtenc="-c"
. But your problem is thatvalue
variable: since you lump everything into one variable (that you quote to pass to rsync), rsync sees one single parameter string ("-c -v -d -r"
) that it cannot parse. Use the individual$c/$d/$v/$r
variables directly in the rsync call.for x in "a b";do echo $x;done
v.s.for x in "a" "b";do echo $x;done
.