LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-30-2018, 04:23 AM   #31
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 834

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365

The ls does not show a given trailing /
so the folder name is the last field; in awk it is
Code:
awk -F/ '{print $NF}'
 
Old 03-30-2018, 07:14 AM   #32
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,226

Rep: Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709
Oh I was testing locally with an ls alias which has display options (adding trailing / for directories)

I think with ' ls -1td /dir/*/ ' it will still display one trailing /
in this case
Code:
awk -F/ '{print $(NF-1)}'
 
Old 03-30-2018, 08:27 AM   #33
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
this one works

Code:
awk -F/ '{print $(NF-1)}'
under the results i am getting (#?):

1) folder1
2) folder2
3) folder3
#?

Don't know why i don't see on the code the #? to replace it with my message like: Please enter your selection:

Also if i use something like:

Code:
select dir in $(ls -1td /folderx/*/ | awk -F/ '{print $(NF-1)}'); do
    echo $dir;
done
read answer
case "$answer" in
 1) do this

How it will know if number 1 is folder1 or folder2 or folder3 ?


Thank you !!!!
 
Old 03-30-2018, 09:21 AM   #34
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,226

Rep: Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709
Please read the link I posted before with select menu examples
eg to set prompt message
Code:
PS3="Please enter your selection:"
select dir in ...
 
Old 03-30-2018, 10:46 AM   #35
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
Working great

Quote:
PS3="Please enter your selection:"
select dir in $(ls -1td /folderx/*/ | awk -F/ '{print $(NF-1)}'); do
echo $dir;
done
Did you post something about the case? Didn't find it sorry

now i am at:

Quote:
PS3="Please enter your selection:"
select dir in $(ls -1td /folderx/*/ | awk -F/ '{print $(NF-1)}'); do
echo $dir;
done
read answer
case "$answer" in
1) do this
but if i select for example 1 i don't know how to get the folder name next to 1) folder1 so i can create a variable for it like:

Quote:
PS3="Please enter your selection:"
select dir in $(ls -1td /folderx/*/ | awk -F/ '{print $(NF-1)}'); do
echo $dir;
done
read answer
case "$answer" in
1) cd $answer <---and the result to be: cd /folderx/folder1/
2) cd $answer <---and the result to be: cd /folderx/folder2/

Last edited by bmxakias; 03-30-2018 at 10:47 AM.
 
Old 03-30-2018, 11:33 AM   #36
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,226

Rep: Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709
Yes I posted about case too, see:
https://www.linuxquestions.org/quest...ml#post5837044

Code:
PS3="Please enter your selection:"
select dir in $(ls -1td /folderx/*/ | awk -F/ '{print $(NF-1)}'); do
  case $dir in
    "folder1")
      echo "folder1... really? Very nice choice."
      break;;

    *) 
      echo "blabla to say with uncovered case..."
      break;;
  esace
done
 
Old 03-30-2018, 11:52 AM   #37
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
If i am not wrong that will work if i answer with the name of the folder for example: folder1 but what i am looking for is to be able to answer using the numbers in front of folders like 2 for example and from that to get the folder2 as variable.....
 
Old 03-30-2018, 12:15 PM   #38
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,226

Rep: Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709
No, just test it. You enter a number as choice in select menu....

After that the corresponding value is stored in $dir variable
 
Old 03-30-2018, 01:41 PM   #39
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
It seems that i confuse you

Sorry.

so by using:

Code:
PS3="Please enter your selection:"
select dir in $(ls -1td /folderx/*/ | awk -F/ '{print $(NF-1)}'); do
  case $dir in
    1)
      echo "folder1... really? Very nice choice."
      break;;

    *)
      echo "blabla to say with uncovered case..."
      break;;
  esac
done
if i select 1 from the menu i am getting: blabla to say with uncovered case...

Last edited by bmxakias; 03-30-2018 at 01:54 PM.
 
Old 03-30-2018, 01:57 PM   #40
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
Ok got it:

Code:
PS3="Please enter your selection:"
select dir in $(ls -1td /folderx/*/ | awk -F/ '{print $(NF-1)}'); do
  case $dir in
    folder1)
      echo "folder1... really? Very nice choice."
      break;;

    *)
      echo "blabla to say with uncovered case..."
      break;;
  esac
done
That works but the issue is that i can't use it as it is as the folders names are changing and are not the same so i can not use as a case a folder name

Last edited by bmxakias; 03-30-2018 at 02:03 PM.
 
Old 03-30-2018, 03:30 PM   #41
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,226

Rep: Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709
The directory name is stored in $dir after the selection
Code:
PS3="Please enter your selection:"
select dir in $(ls -1td /folderx/*/ | awk -F/ '{print $(NF-1)}'); do
  break
done

# do whatever you want with $dir
echo "Hello, I am a directory and my name is: $dir"
echo "Come visit me"
cd /folderx/$dir

echo "Have a look here"
ls
 
1 members found this post helpful.
Old 03-31-2018, 11:52 AM   #42
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
Perfect thank you so much !!!!

I think that the way that it works now i may not need to use the case option...

Last edited by bmxakias; 03-31-2018 at 11:54 AM.
 
Old 04-02-2018, 03:21 PM   #43
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
Using the code:

Code:
PS3="Please enter your selection:"
select dir in $(ls -1td /folderx/*/ | awk -F/ '{print $(NF-1)}'); do
  break
done

the result is:

Code:
1) folder1
2) folder2
Please enter your selection:
I just try to place echo "" on all possible places to get it as:

Code:
1) folder1
2) folder2

Please enter your selection:
but no luck

Any help please?

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

Rep: Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709
Change PS3 to:
Code:
PS3="
Please enter your selection:"
 
Old 04-02-2018, 04:55 PM   #45
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
Last one:

How can i allow only numbers as an input?

I found this but don't know how to apply it:

Code:
~ ^[0-9]+$
What i want to avoid is when i have:

Code:
1) foldera
2) folderb
to not allow special characters and letters and also to not allow a number that doesn't exist like 4 to get as an input selection.

Last edited by bmxakias; 04-02-2018 at 08:02 PM.
 
  


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 03:58 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