How to compare the user input in bash scripting?
Hello
I have some folders like: Code:
/backup/$DOMAIN/$DATE/database//inc/inc1 Code:
echo "" Code:
Available increamental backups : Now what i would like to do is to get the input from the user selection (probably using the read command? ) and if the user select: 1 to run a command for folder inc1 2 to run a command for folder inc1 and then inc2 3 to run a command for inc1 and then inc2 and then inc3 But the problem is that what i will have in 1) or 2) or 3) may not be always the same... So i must be able to read if it is just inc1 and run a simple cmmand or inc3 and run the 3 needed commands.... Is it possible to do that? To work it must go from inc1 up to selected inc(x) that the user will select.. Thank you |
yeah, you need to use a bash builtin called select.
We did some crafty things with that the other day I'll find the link and edit this post for you Edit https://www.linuxquestions.org/quest...ds-4175659851/ you should get what you need from that post what you come up if you get stuck |
Use $domainbackupdate. Its value will be one of the items in the output of the ls command i.e. inc1,inc2 or inc3 in your posted example. So the solution is easier then you think.
There are lots of string manipulation tools to separate the number from inc and then you can use a loop to go from inc1 to the selected inc(x). https://bash.cyberciti.biz/guide/Select_loop |
Quote:
I'll hunt for a simple example of this approach and post it if anyone's interested in seeing it. (Right now I'm off to the hardware store to polish of couple of tasks in the Job Jar. :^) ) |
I think that all solutions are related on how to get the user input and act on that....but on my case i need the script to understand the input number of the folder name like inc3 and then then to run a command for inc1 and then for inc2 and then for inc3 and then stop.
Maybe my knowledge is limited on that but i can't see on the provided links that functionality ... Maybe i didn't explain it well ? :( Thanks for your replies ! |
To explain it a bit better...
If user select the folder inc1 that can have any number at the selection menu i would like to run this command: Code:
touch /backup/$DOMAIN/$DATE/database/inc/inc1/file.txt Code:
touch /backup/$DOMAIN/$DATE/database/inc/inc1/file.txt |
in the link I posted we talked about how to decide what to do once the user had selected the option
so if user selects inc3 to recreate that you first restore inc1, then inc2 and finally inc3 if user selects inc2 to recreate that you first restore inc1 and finally inc2 if user selects inc1 restore inc1 ok you built an array starting at 0 0=inc1 1=inc2 2=inc3 then with select presented it as 1) 2=inc3 2) 1=inc2 3) 0=inc1 Code:
Count of backups is 3 Code:
Dirs=## array comes from code I wrote in the other thread does that make sense? Note : /!\ things like that are out by 1 if they are wrong ;) |
Code:
#!/bin/bash |
If you choose inc3 the value of $domainbackupdate will equal inc3. Using a substring function you can assign a variable to equal 3 in this case.
From there you can create a loop from 1 to 3 to create the file.txt files in each incx directory without knowing the order of the select menu. |
yeah true
Code:
select CHOICE in "${Dirs[@]}";do you can get the digit from the string and reconstruct, but you already have all that bash will have globbed in numerical order reconstructing negates the need for 000pad but the order things are presented in will look messy 000pad and reconstruct needs more work to strip the leading 0s ( 08 != 8 ) gaps ( inc1 inc3 # inc2 is missing ) will break .. well both I guess the reconstructed won't find inc2 with the array the restore will fail ( I assume inc3 depends on inc2) This is where metadata needs to be put in the backups, a first pass checks the dependencies and then restores once it confirms they are satisfied which makes it even easier, since inc3 we check inc3/metadata and it says well I need inc2 first, inc2/metadata says well, I need inc1 first so we only need to run restore on "${Dirs[$(( $REPLY - 1 ))]}" does that make sense? |
Thanks for your replies !!!!
The inc folders may be up to 46 .... Can you please adjust your code for that so i can see the difference ? Also if it is easier to do it by changing anything in the paths names, it's ok to do that... For example if 1inc, 2inc or 1-inc, 2-inc is better for the script..... |
you need to pad the 1 2 3 with leading zeroes 001 002 003 .. 999 1000 /!\ 1000 breaks it
you could use printf to pad revisit the creation part https://www.linuxquestions.org/quest...me-4175660121/ This is inspired by GazL's post I added option to order Ascending/Descending Code:
#!/bin/bash |
Can you please remove from your code the creation of the folders (as i do that from the backup script) and i already adjust it and now i have:
Code:
/backup/$DOMAIN/$DATE/database//inc/inc001 Also Ascending/Descending doesn't matter .... we may remove that also.... I just need to list folders and get the user input ... With the above code i am getting the Ascending/Descending menu but i don't see anything listed to select and see if it works... Thank you |
N/M
|
:D
bless I was just trying to inspire , not actually write it for you look at them as working examples, not working scripts. out of interest, how did you pad with 00? |
Code:
while [[ -d "$INCDIR/inc$N" ]] ; do Which inc folder you want to use bla bla bla but again if the user type inc001 or inc004 i must find a way to identify that and run for inc001 one command and for inc004 four commands for each folder inc001, inc002, inc003 and inc004.... Don't know.... |
Quote:
Code:
N=$(($N+1)) |
what happens whey you get to 10?
|
exactly I was just thinking the 'same thing'. when you while loop finds last dir it stops incrementing at that number, then your next operation is to do something with a link and a number attached to it using $N, but mkdir *$N = last number found. ending with dir already present message.
or being that between you check without leading 00 and mkdir having leading 00. you know what you're doing over what I am speculating. but Firerat might be referring to 009 0010 apposed to 009 010 |
Quote:
edit, well 001 0010 0011 002 009 kinda messed up if you want order |
After testing the code with 001 and ((N++)) the only working with no issues and up to 20 that i tested with no issues is this code:
Code:
while [[ -d "$INCDIR/inc$N" ]] ; do Now i am thinking to find a way to compare the input like: check if user selected inc1 and if yes do this. If user selected inc2 do that and so on.... As there will not be more than 23 folders.... So maybe you can help me on that? How to compare user input (use read to get it) with inc1 inc2 and so on ? Thank you ! |
Quote:
Code:
for i in `seq -w 1 100 | sed -n '1,100p'`; |
Code:
mkdir -p "foo/bar/inc$( printf "%03d" ${N} )" Quote:
you can skip that in the code reverse order? Code:
#select order in Ascending Descending;do https://www.linuxquestions.org/quest...8/#post6032085 |
I think that will work:
Code:
echo -e "Please type your inc folder name: \c" But how can i do that to avoid repeating this code 23 times? :) |
Quote:
you can run script. script gets list of dirs already created. puts them into an array, prints out results, ask user what dir he wants to back up ( I think that is whats going on here.) then using case statement find match then issue commands to back it up. if you use functions you can incorporate flags to call the functions so your entire created and back up can be in the same script. |
Just edited my message above:
I think that will work also: Code:
echo -e "Please type your inc folder name: \c" But how can i do that to avoid repeating this code 23 times? :) What do you think about that? Thank you |
find /backupFolders > makealist
print list ask user what number he wants take that and go from there |
yeap
Regards the restore what happens if one of the incremental backups is missing? how are these inc. backups being made? do you really need to restore 1 and 2 to get 3? i.e., when you create backup inc1 that is a copy of original... inc2 that is the difference between inc1 and the Current inc3 that is the difference between inc1+inc2 and the Current That is how I understand it based on the required restore inc1 then inc2 then inc3 Or, is each one a snapshot in time? the former is more space efficient but more complicated, and risk of failure increases each backup ( since the last one depends on all the ones before it ) the later is far easier, but uses more space |
this is just an working example of known var / dir names
Code:
baseDir=$HOME/test gets count then using the known format shows amount up to last dir created to its 100 but removing the cat and only using the echo you'll get a sequence of start to last to pick from. select inc001 - inc100 |
Quote:
Code:
#!/bin/bash and no clean up |
Quote:
|
basic dir creation
Code:
#!/bin/bash 1, 2, 4, 5 recreates 5 we could check the array with something like this Code:
foo=( "inc001" "inc002" "inc004" ) |
All times are GMT -5. The time now is 08:19 AM. |