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 02-18-2010, 07:45 AM   #1
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

Rep: Reputation: 38
Howto | an array of characters


I am trying to create an array containing all ASCII characters, how do I create one:

Code:
#!/bin/bash
CHARLIST=( 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 \
           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 \
           0 1 2 3 4 5 6 7 8 9 \
           ~ ! @ # $ % ^ & * ( ) _ + ` - = \ 
           { } | [ ] \ : " ; ' < > ? , . / \
         )

for char in ${CHARLIST[@]}
do
  echo ${char}
done
exit 0
I am having problems with the # character and the characters of the last line of the array.
 
Old 02-18-2010, 08:08 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
For starters, what problems are you having?

Keep in mind that many of these characters have a special meaning (which in turn depends on context).

I tried putting all the characters in a file ("asc") and then ran:
Code:
while read char; do echo $char;done <asc
It appears the only issue was that "*" was expanded to the contents of the current working directory.
 
Old 02-18-2010, 08:19 AM   #3
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

Original Poster
Rep: Reputation: 38
This is the output when I execute the script:

Code:
./test.sh: line 11: syntax error near unexpected token `|'
./test.sh: line 11: `           { } | [ ] \ : " ; ' < > ? , . / '
./test.sh: line 12: syntax error near unexpected token `)'
./test.sh: line 12: `         )'
Besides, the # character in the array is commenting out the rest of the line.
 
Old 02-18-2010, 08:34 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
This appears to work:

Code:
#!/bin/bash

set -f

CHARLIST=( 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 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 0 1 2 3 4 5 6 7 8 9 \~ \! \@ \# \$ \% \^ \& \* \( \) \_ \+ \` \- \= \{ \} \| \[ \] \\ \: \" \; \' \< \> \? \, \. \/ )

for char in "${CHARLIST[@]}"
do
  echo "${char}"
done

exit 0
First of all, I got rid of the line-breaks, which made it confusing to work with.
Then "set -f" turns off globbing in bash so * doesn't expand to the directory contents. (Edit: quoting "${CHARLIST[@]}" in the loop means not having to fool with the globbing setting.) Finally backslashing all of the non-alphanumeric characters avoids any shell interpretation (probably not all of them need escaping, but it doesn't hurt).

Last edited by David the H.; 02-18-2010 at 08:48 AM. Reason: update
 
Old 02-18-2010, 08:34 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

@pixellany: If you put $char between double quotes the * is not expanded: while read char; do echo "$char"; done < asc

@the_gripmaster: Did you have a look at pixellany's comment? Your array has all sorts of characters that need to be escaped (the CHARLIST variable won't be filled because of this as stated by the errors shown).
 
Old 02-18-2010, 08:44 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
By the way, you can generally shorten up the string some by using brace expansion.
Code:
CHARLIST=( {a..z} {A..Z} {0..9} \~ \! \@ \# \$ \% \^ \& \* \( \) \_ \+ \` \- \= \{ \} \| \[ \] \\ \: \" \; \' \< \> \? \, \. \/ )
I wouldn't trust it to reliably reproduce the entire ascii set though, since the sort order relies on your locale settings.
 
1 members found this post helpful.
Old 02-19-2010, 01:06 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Code:
CHARLIST=( {a..z} {A..Z} {0..9} \
           #  ~  ! @  #  $ % ^  &  *  (   )  _ +  `  - = \
             '~' ! @ '#' $ % ^ '&' . '(' ')' _ + '`' - = \
           # { }  |  [ ]  \  :  "   ;   <   >  ? , . / \
             { } '|' [ ] '\' : '"' ';' '<' '>' ? , . / \
         )
CHARLIST[70]='*'

set -f  # To prevent * being expanded
for char in ${CHARLIST[@]}
do
  echo -n "$char "
done
echo

exit 0
EDIT: CHARLIST[70]='*' is not required. * can be set along with the other characters as '*'.

If you do want the entire ASCII set you could use a loop with CHARLIST[$i]=$'\<$i in octal>'

Last edited by catkin; 02-19-2010 at 01:15 AM.
 
  


Reply

Tags
bash



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
Help SSH into logfile and search with array loop of wild card characters gatorpower Programming 1 12-09-2009 10:20 AM
LXer: Mastering Characters Sets in Linux (Weird Characters, part 2) LXer Syndicated Linux News 0 11-25-2009 11:30 PM
splitting numbers and characters from an array of characters. trscookie Programming 6 11-14-2008 09:34 AM
mirror characters in an array flyordie Linux - Newbie 1 10-16-2008 04:07 AM
Mount a raid array with SGID bit set, howto? anand_kt Linux - General 4 04-01-2005 01:30 AM

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

All times are GMT -5. The time now is 10:21 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