LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-18-2014, 02:52 AM   #1
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,465

Rep: Reputation: Disabled
Bash: How to increment an alphabetic value of a variable alphabetically?


So for numbers it's easy.
Code:
PAGE=1; ((PAGE++)); echo $PAGE
and you get 2.

What about this?
Code:
LETTER=A; ????; echo $LETTER
And the answer is B?
jdk
 
Old 12-18-2014, 03:17 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
use perl

Code:
perl -e '$x=A;print $x++, "=", $x,"\n"'
A=B
 
Old 12-18-2014, 04:12 AM   #3
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
If you want to do it in a loop try:

Code:
for letter in {A..Z} ; do
  echo ${letter}
done
 
Old 12-18-2014, 10:41 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Code:
function NextLetter () {
    local Rest Letters=ABCDEFGHIJKLMNOPQRSTUVWXYZ
    Rest=${Letters#*$1}
    echo ${Rest:0:1}
}
 
Old 12-18-2014, 12:27 PM   #5
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,465

Original Poster
Rep: Reputation: Disabled
Thanks for your efforts. Not really what I want. I have a bash script here:
Code:
#!/bin/bash
LEFT="http://www.xxx.xxx/languages-pronunciations/de/alphabetically/L/page-"
PAGE=1
URL="$LEFT$PAGE"
print $RIGHT
while wget $URL; do
((PAGE++))
URL="$LEFT$PAGE"
done
This involves getting a series of web pages containing lists of words. They are organised by page number which is why I have a loop to increment the PAGE variable to get a different page for each loop.
and so forth.
The pages are also organised by the initial letter of the word shown in bold above. So in the example above I would want to increment the "L" to "M" when all the words in "L" have been exhausted.
I hope that's clearer.
jdk

Last edited by jdkaye; 12-18-2014 at 12:29 PM.
 
Old 12-18-2014, 01:41 PM   #6
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by jdkaye View Post
Thanks for your efforts. Not really what I want.
So what DO you want? Someone to write the script for you?

You asked how to go from letter to letter, you've been given that, any of those techniques could be used as an "outer loop" to go through letters with your inner loop doing the page stealing.
 
Old 12-18-2014, 02:41 PM   #7
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,465

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TenTenths View Post
So what DO you want? Someone to write the script for you?

You asked how to go from letter to letter, you've been given that, any of those techniques could be used as an "outer loop" to go through letters with your inner loop doing the page stealing.
Obviously not. I wrote the entire script I have shown. Given the same question concerning numerical rather than alphabetic incrementing, the response would be, "Take a variable X and place it in the expression "((X++))" and its value will be incremented by one each time it's called. I hope that's clear enough for you concerning what I expect.

jdk
 
Old 12-18-2014, 03:12 PM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
With a suitable replacement for LEFT, this should do what you want.

Code:
#!/bin/bash

LEFT="http://www.xxx.xxx/languages-pronunciations/de/alphabetically/"

for LETTER in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z; do
PAGE=1
while wget "$LEFT$LETTER/page-$PAGE"; do
((PAGE++))
done
done
Now it does what you want...

Here is the same loop modified to only echo URL and fetch 3 pages for each letter to test it.

Code:
LEFT="http://www.xxx.xxx/languages-pronunciations/de/alphabetically/"

for LETTER in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z; do
PAGE=1
while [[ PAGE -lt 4 ]]; do
URL="$LEFT$LETTER/page-$PAGE"
echo "$URL"
((PAGE++))
done
done

Last edited by astrogeek; 12-18-2014 at 03:28 PM.
 
Old 12-18-2014, 03:25 PM   #9
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
So with the information already in this thread and two lines added to your script to handle going from L to N, as I couldn't be bothered downloading more because 3 letters is enough to prove my letter incrementing works (and with a line to only download 10 pages max) I can download the following files:

Code:
-rwxr-xr-x 1 jdkaye jdkaye   376 Dec 18 21:16 dumbass.sh
-rw-r--r-- 1 jdkaye jdkaye 39714 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-1
-rw-r--r-- 1 jdkaye jdkaye 40534 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-10
-rw-r--r-- 1 jdkaye jdkaye 40213 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-2
-rw-r--r-- 1 jdkaye jdkaye 41005 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-3
-rw-r--r-- 1 jdkaye jdkaye 41097 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-4
-rw-r--r-- 1 jdkaye jdkaye 41041 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-5
-rw-r--r-- 1 jdkaye jdkaye 41027 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-6
-rw-r--r-- 1 jdkaye jdkaye 40427 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-7
-rw-r--r-- 1 jdkaye jdkaye 40963 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-8
-rw-r--r-- 1 jdkaye jdkaye 40396 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-9
-rw-r--r-- 1 jdkaye jdkaye 40134 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-1
-rw-r--r-- 1 jdkaye jdkaye 41424 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-10
-rw-r--r-- 1 jdkaye jdkaye 40285 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-2
-rw-r--r-- 1 jdkaye jdkaye 40515 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-3
-rw-r--r-- 1 jdkaye jdkaye 40851 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-4
-rw-r--r-- 1 jdkaye jdkaye 40983 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-5
-rw-r--r-- 1 jdkaye jdkaye 40913 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-6
-rw-r--r-- 1 jdkaye jdkaye 41041 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-7
-rw-r--r-- 1 jdkaye jdkaye 41107 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-8
-rw-r--r-- 1 jdkaye jdkaye 40440 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-9
-rw-r--r-- 1 jdkaye jdkaye 40337 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-1
-rw-r--r-- 1 jdkaye jdkaye 41055 Dec 18 21:17 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-10
-rw-r--r-- 1 jdkaye jdkaye 40466 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-2
-rw-r--r-- 1 jdkaye jdkaye 41088 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-3
-rw-r--r-- 1 jdkaye jdkaye 41548 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-4
-rw-r--r-- 1 jdkaye jdkaye 41450 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-5
-rw-r--r-- 1 jdkaye jdkaye 41212 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-6
-rw-r--r-- 1 jdkaye jdkaye 41118 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-7
-rw-r--r-- 1 jdkaye jdkaye 40908 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-8
-rw-r--r-- 1 jdkaye jdkaye 40969 Dec 18 21:17 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-9
Quote:
Originally Posted by jdkaye View Post
I hope that's clear enough for you concerning what I expect.

So yeah, I kinda get what you expect.
 
Old 12-18-2014, 03:27 PM   #10
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by astrogeek View Post
With a suitable replacement for LEFT, this should do what you want.

Code:
#!/bin/bash

LEFT="http://www.xxx.xxx/languages-pronunciations/de/alphabetically/"

for LETTER in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z; do
PAGE=1
while wget "$LEFT$LETTER/page-$PAGE"; do
((PAGE++))
done
done
Now it does what you want...

Awww, you wrote it for him, how generous


Personally I used
Code:
for LETTER in {A..Z} ; do
Same result, less to type
 
Old 12-18-2014, 03:33 PM   #11
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Quote:
Originally Posted by TenTenths View Post
Awww, you wrote it for him, how generous
Sometimes, that is just what people need.

jdkaye has been here long enough to deserve that much respect.

Quote:
Originally Posted by TenTenths View Post
Personally I used
Code:
for LETTER in {A..Z} ; do
Same result, less to type
That is probably because you are a better bash scripter than I am! (I also used your outer loop, obviously)

Last edited by astrogeek; 12-18-2014 at 03:35 PM.
 
Old 12-19-2014, 01:44 AM   #12
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,465

Original Poster
Rep: Reputation: Disabled
Thank you all very much.
(1) I wanted to use bash because beyond very simple bash scripts (I've written loads) I haven't done much bash writing.
(2) My real question was, "Does an alphabetic function exist in bash. I did not ask for nor did I expect anyone to write it for me in the event that such a function is not built into bash. I downloaded the bash manual (4.2) and did a thorough search of that manual before bothering members of this august group. I could find the numeric increment function (shown in an earlier post) and happily used it. I thought perhaps an alphabetic increment function did exist but I was too thick to find it in the manual so I asked here.
(3) I am fully aware that one can write a function doing that job but that is not what my question was and not what I expected. A simple "no, but you can write one yourself" would have sufficed.
(4) As for using Perl, one of the points of my task was teaching myself more about bash. If I wanted to do the same thing using another language I would have chosen Icon which is the language I'm most familiar with.
(5) Finally, I think I'll explore the unicode or ascii route. Now, I'll see if there's a bash function that converts octals/hexadecimals numbers to decimals and vice versa. But don't worry; I won't ask. I wouldn't want you to think I'm asking you to write my code for me.
Thank you all once again.
jdk
 
Old 12-19-2014, 02:22 AM   #13
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Focusing on post #5 I thought that your ultimate goal was to automate the download, not just to find a built-in alpha-increment feature of bash.

I did not immediately know how to best do that so thought I would learn by doing it - and I gained something by that.

I did not consider it as writing your script for you, or that you had not "done enough" on your own - who am I to make such a judgment anyway?!

My own intent was only to be helpful. Sorry if it was taken any other way, definitely not intended.

Best of luck in your explorations!
 
Old 12-19-2014, 02:26 AM   #14
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by astrogeek View Post
Sometimes, that is just what people need.
Nah, never!
Quote:
Originally Posted by astrogeek View Post
jdkaye has been here long enough to deserve that much respect.
Fair point!
Quote:
Originally Posted by astrogeek View Post
That is probably because you are a better bash scripter than I am!
Unlikely!
I just happened to have spent the last few days tidying up other peoples bash scripts including dealing with alphabet loops and date loops.
 
Old 12-19-2014, 02:34 AM   #15
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by jdkaye View Post
I'll see if there's a bash function that converts octals/hexadecimals numbers to decimals and vice versa.
jdk
bash builtin printf does it. or dc

But, forcing shell for tasks it's not easy to do is just giving yourself a headache is it not?
 
  


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
Increment in bash graphicsmanx1 Programming 18 11-23-2012 10:23 PM
[SOLVED] Bash Script to increment eyanu Linux - Newbie 2 11-16-2012 05:10 AM
[Bash] increment number in a string czezz Programming 4 07-01-2009 11:34 AM
bash equivalent to C increment operator andrewb758 Linux - General 5 02-21-2009 12:25 PM
Checking whether a variable is alphabetic basildon Linux - Newbie 4 09-21-2008 04:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:56 PM.

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