LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-13-2019, 04:08 PM   #1
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Rep: Reputation: Disabled
Question Simple bash script allow me to select only 1 from the list


Hello

I am trying to create a backup script and i am using this:

Code:
Please select to which backup date [D-M-Y] you want to restore [1, 2, 3 e.t.c]:"
# get the folders count
count=$(ls -1td /home/nginx/domains/*/ | wc -l)

select domainbackupdate in $(ls -1td /backup/${domaintorestore}/*/ | awk -F/ '{print $(NF-1)}'); do
  # if input is a number and is less than or equal to count
  if (( $REPLY && $REPLY <= $count )) 2> /dev/null; then
    break
  else
    echo "Your input ${REPLY} is not available. Please select a number from the list:"
  fi
done
so that displays something like this:

Code:
1) 13-01-2019    5) 09-01-2019
2) 12-01-2019    6) 08-01-2019
3) 11-01-2019    7) 07-01-2019
4) 10-01-2019    8) 06-01-2019
but i can select only 1 and no 2 or 3

Any help please?
 
Old 01-13-2019, 06:07 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
What is the result of
Code:
ls -1td /home/nginx/domains/*/ | wc -l
?
 
Old 01-13-2019, 06:58 PM   #3
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
the result is:

Code:
1
and it displays my domain:

domain.com

and if i select 1 the domain is selected...

I think the problem is my answer to this:

Code:
select domainbackupdate in $(ls -1td /backup/${domaintorestore}/*/ | awk -F/ '{print $(NF-1)}'); do

Last edited by bmxakias; 01-13-2019 at 07:01 PM.
 
Old 01-13-2019, 07:20 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Code:
count=$(ls -1td /home/nginx/domains/*/ | wc -l)
Code:
ls -1td /backup/${domaintorestore}/*/
For your script to work count has to equal the number of directories in /backup/${domaintorestore} not /home/nginx/domains/*/. If you want to select domain to restore your asking the wrong question.
 
1 members found this post helpful.
Old 01-13-2019, 07:30 PM   #5
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
When i run the script i am getting this:

Code:
Available domains on the system:

1) mydomain.com


Please select which domain you want to restore [1, 2, 3 e.t.c]:
So i select 1 and it is ok.

then i am getting this:

Code:
Available backups by date for mydomain.com :

1) 13-01-2019    5) 09-01-2019   9) 05-01-2019  13) 01-01-2019  17) 28-12-2018
2) 12-01-2019    6) 08-01-2019  10) 04-01-2019  14) 31-12-2018  18) 27-12-2018
3) 11-01-2019    7) 07-01-2019  11) 03-01-2019  15) 30-12-2018  19) 26-12-2018
4) 10-01-2019    8) 06-01-2019  12) 02-01-2019  16) 29-12-2018  20) 25-12-2018


Please select to which backup date [D-M-Y] you want to restore [1, 2, 3 e.t.c]:
And here i can select only 1 that is related to 13-01-2019 and not be able to select 2 or 3 for example....
 
Old 01-13-2019, 07:36 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
What michaelk said...$count is the count of domains, not the count of available backups. So when you test against $count, only 1 is allowed.
Reset $count (or use another variable) to contain the number of available backups.

Last edited by scasey; 01-13-2019 at 07:37 PM.
 
Old 01-13-2019, 07:57 PM   #7
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
Oops i just notice it

Thank you both for your help !!!
 
Old 01-14-2019, 03:44 AM   #8
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
^ If you think your problem is solved now, can you mark your thread as such please?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Ok to select only 64-bit architecture in Debian Source list? linustalman Debian 9 02-24-2017 10:40 AM
A simple script that can scroll down a list and select one line. xmrkite Linux - Software 4 06-15-2009 03:43 PM
[SOLVED] Using hosts.allow to allow only postfix to use port 25 Mogget Linux - Server 3 03-28-2009 04:36 AM
Bash script: how do I select second-to-last argument in a list Robert S Linux - Software 2 11-23-2007 03:06 PM
hosts.allow file to allow only a single IP sailu_mvn Linux - Networking 2 06-06-2007 11:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration