LinuxQuestions.org
Help answer threads with 0 replies.
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 04-03-2018, 11:57 AM   #46
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,226

Rep: Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709

Code:
ls -1td /folderx/*[0-9]/
May need to add others [0-9] at the end if your names end with double digits or more
 
Old 04-03-2018, 12:05 PM   #47
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
I am using your code for two paths and one path always have names like hello.com and the other one numbers like 23-03-2018 ....

Last edited by bmxakias; 04-03-2018 at 12:08 PM.
 
Old 04-03-2018, 01:21 PM   #48
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,226

Rep: Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709
Sorry, I didn't carrefully read your folder names restriction
Code:
ls -1td /folderx/folder[a-z]/
 
Old 04-03-2018, 01:58 PM   #49
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
Thanks for your reply and sorry for my bad english

I think i confuse you.

The command doesn't work:

Code:
ls -1td /folderx/folder[a-z]/
I think it's normal as none of the folders inside the folderx are starting by name "folder" and all are random names.

Also i am getting the error:

ls: cannot access /folderx/folder[a-z]/: No such file or directory

It is clear that is trying to list the "folder[a-z]" as a folder name so that's the issue (i try it also as /folderx/[a-z]/ )

Ok it's not big deal if we can't get it work so don't worry


I will post some more details and try to explain more:

I am using it as:

PS3="

Please enter your selection [1, 2, 3 e.t.c]:"
select domaintorestore in $(ls -1td /folderx/*/ | awk -F/ '{print $(NF-1)}'); do
break
done

and that folder provides folder names like:

1) name.com
2) something.net
3) 123.anotherone.org

And the user must select using 1 or 2 or 3 but that may go up to a much higher number.

Now what i want is to not allow the user to add as an input any special character or letters as the selection must be only a number and a number only listed and not something like 168

Thank you !!!!
 
Old 04-03-2018, 02:37 PM   #50
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,226

Rep: Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709
Ah yes, but in this case you need to check the input inside the select loop
ie: don't do a break if input is not correct

What is a correct input? A number obviously but also a number that doesn't exceed the folders count

Code:
# get the folders count
count=$(ls -1td /folderx/*/ | wc -l)

select domaintorestore in $(ls -1td /folderx/*/ | awk -F/ '{print $(NF-1)}'); do
  # if input is a number and is less than or equal to count
  if (( $REPLY && $REPLY <= $count )); then
    break
  else
    echo "Error: wrong value, press enter to display the list again"
  fi
done

Last edited by keefaz; 04-03-2018 at 02:38 PM.
 
Old 04-04-2018, 07:12 AM   #51
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
It is working great for existing numbers and report invalid numbers and letters and it doesn't allow special characters also

For special characters only it reports an error:

When i try to use: #

Code:
./test.sh: line 38: ((: # && # <= 1 : syntax error: operand expected (error token is "# && # <= 1 ")
Thank you !

Last edited by bmxakias; 04-04-2018 at 07:38 AM.
 
Old 04-04-2018, 09:17 AM   #52
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,226

Rep: Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709
To suppress error message from condition:
Code:
if (( $REPLY && $REPLY <= $count )) 2> /dev/null; then
 
1 members found this post helpful.
Old 04-04-2018, 02:46 PM   #53
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
Working great !!!!

Thank you so much for your time/knowledge
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash Script to add numbers which has 0 prefix kingston Linux - Newbie 15 07-18-2015 01:23 PM
[SOLVED] Bash script: comparison with sequence of numbers? dbrazeau Programming 11 03-22-2011 12:22 PM
bash script to create folders including making recursive folders.... linux-bandit Linux - Software 8 11-28-2009 01:50 AM
Bash command to list installed libraries and version numbers newtovanilla Linux - Newbie 4 07-18-2008 04:49 PM
Decimal numbers in bash script variables? Massif Programming 3 11-07-2005 09:01 PM

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

All times are GMT -5. The time now is 06:36 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration