- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Increasing ASCII values in shell variables
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-09-2010 09:30 AM
тАО01-09-2010 09:30 AM
I have 2 questions that concerns giving ASCII values to shell variables. I need it for loop that increases the ASCII value of a variable each loop pass. I need something like that (I will use it for grep not echo):
$ for right_char in A B C D E
> do
> echo MYVAR${right_char}
> done
MYVARA
MYVARB
MYVARC
MYVARD
MYVARE
However, if I want the loop to go on on the letters from A to Z, I would need either:
1. A way to tell the for to go from A to Z, something like [A..B], is there such a construct?
2. Define right_char to hold ASCII code that I can increase with each loop pass with the simple + operator. What is the way to do that?
The shell is a POSIX shell.
Thanks for the answers.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-09-2010 09:40 AM
тАО01-09-2010 09:40 AM
Re: Increasing ASCII values in shell variables
You could use a tiny Perl snippet:
# perl -le 'print "MYVAR$_" for (A..Z)'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-09-2010 01:36 PM
тАО01-09-2010 01:36 PM
Re: Increasing ASCII values in shell variables
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-09-2010 02:39 PM
тАО01-09-2010 02:39 PM
Solution> I actually need to use it in a grep command, not echo.
If I understand correctly, you might do something like:
#!/usr/bin/sh
VARS=$(perl -le 'print "MYVAR$_" for (A..Z)')
for X in ${VARS}
do
grep ${X} file
done
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-09-2010 03:51 PM
тАО01-09-2010 03:51 PM
Re: Increasing ASCII values in shell variables
$ echo 'abcdefghijklmnopqrstuvwxyz' | sed -e "s/.\{${n}\}\(.\).*/\1/"
a
$ n=1
$ echo 'abcdefghijklmnopqrstuvwxyz' | sed -e "s/.\{${n}\}\(.\).*/\1/"
b
[...]
$ n=24
$ echo 'abcdefghijklmnopqrstuvwxyz' | sed -e "s/.\{${n}\}\(.\).*/\1/"
y
$ n=25
$ echo 'abcdefghijklmnopqrstuvwxyz' | sed -e "s/.\{${n}\}\(.\).*/\1/"
z
It's easier in DCL:
alp $ n = 12
alp $ write sys$output f$extract( n, 1, "abcdefghijklmnopqrstuvwxyz")
m
Or, more cryptically:
alp $ chr=""
alp $ chr[ 0, 8] = 97+ n
alp $ write sys$output chr
m
As usual, many things are possible.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-09-2010 07:53 PM
тАО01-09-2010 07:53 PM
Re: Increasing ASCII values in shell variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-09-2010 10:19 PM
тАО01-09-2010 10:19 PM
Re: Increasing ASCII values in shell variables
You could also use an array of chars like Steven's:
set -A chars A B C D E F ...
echo $chars[0]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-10-2010 11:38 AM
тАО01-10-2010 11:38 AM
Re: Increasing ASCII values in shell variables
For just the list shown in the original question:
#!/usr/bin/sh
for CHR 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
echo MYVAR$CHR
done
#################
For a numeric method:
#!/usr/bin/sh
set -A AZ 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
CNT=0
while [ $CNT -le 25 ]
do
echo MYVAR${AZ[$CNT]}
CNT=$((CNT+1))
done
In the second example, the CHR array can be referenced with an number between 0-25. Insert a dummy value before "A" to change the index to 1-26.
The grep requirement is not clear. If you are looking for all matches for MYVAR ending with any char A thru Z, this will do it on one line:
grep MYAR[A-Z] some_file
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-11-2010 06:00 AM
тАО01-11-2010 06:00 AM
Re: Increasing ASCII values in shell variables
# echo {1,2,3}{a,b,c}
1a 1b 1c 2a 2b 2c 3a 3b 3c
#
Unix operates with beer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-11-2010 09:17 PM
тАО01-11-2010 09:17 PM
Re: Increasing ASCII values in shell variables
#!/usr/bin/ksh
(( char = 16#41 ))
while (( char <= 16#5a )); do
awk -v char=$char 'BEGIN {printf "MYVAR%c\n", char }' /dev/null
(( char += 1 ))
done
And you can put the whole loop in awk:
awk '
BEGIN{
for (i = 0; i < 26; ++i)
printf "MYVAR%c\n", i + 65
}' /dev/null