Bash to ash port, character-matching problem


 
Thread Tools Search this Thread
Operating Systems Linux Ubuntu Bash to ash port, character-matching problem
# 1  
Old 07-22-2014
Bash to ash port, character-matching problem

I'm trying to convert this working bash script into an Ash script,

Code:
 read -p "Username:" _username
   if ! [[ "$_username" =~ [^a-zA-Z0-9] ]]; then
   	 echo "Valid"
   else
    	 echo "INVALID"
   fi

However, Ash does not recognize the "=~" character.

How can I do this?
Also, is there a good reference guide, so I don't need to post all my questions here? I can't find anything!

Moderator's Comments:
Mod Comment edit by bakunin: please use the CODE-tags for code and/or terminal output. Thank you.

Furthermore, please place different questions into different threads. It is perfectly OK to open several threads for several distinct problems and i can assure you, we have empty threads in abundance, without any threat of a shortage.

Last edited by bakunin; 07-23-2014 at 11:36 AM..
# 2  
Old 07-22-2014
The bash shell provides lots of extensions above and beyond what the POSIX standards require. The ash shell doesn't provide nearly as many extensions. If you use the standard as your shell programming guide, you should get by OK. A straight translation (getting rid of bash extensions) for your script would be:
Code:
printf "Username:"
read _username
if [ "$_username" = "${_username%[^a-zA-Z0-9]*}" ]
then    echo "Valid"
else    echo "INVALID"
fi

If your system allows alphanumeric characters other than characters in the Portable Filename Character Set in user names, you might want:
Code:
printf "Username:"
read _username
if [ "$_username" = "${_username%[^[:album:]]*}" ]
then    echo "Valid"
else    echo "INVALID"
fi

# 3  
Old 07-23-2014
# 4  
Old 07-23-2014
I'd like to add that in any case, letting variable names start with underscores is a problematic practice, even if it is allowed. Consider:

Code:
_foo="bar"
echo $_foo

_="bar"
echo $_

I hope this helps.

bakunin
# 5  
Old 07-23-2014
Code:
printf "Username:"; read _username
   if [[ "$_username" != *[!a-zA-Z0-9]* ]]; then
      echo "Valid"
   else
      echo "INVALID"
   fi


Last edited by MadeInGermany; 07-23-2014 at 12:55 PM.. Reason: [^ ] replaced by [! ]
# 6  
Old 07-23-2014
A case statement will work in any Bourne type shell. This also makes it easy to test for an empty variable..
Code:
printf "Username: "
read _username
case $_username in 
  (*[![:alnum:]]*|"") 
    echo "invalid" ;;
  (*) 
    echo "valid"
esac

Note that will also allow letters with accents like é and ã
To exclude those, you could use (*[!a-zA-Z0-9]*|"") but that may not work properly because of a locale collation order...

--
Also note hat the proper negation character with character sets used in patterns is ! not ^ (even though it probably will work too)

Last edited by Scrutinizer; 07-23-2014 at 02:34 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash script, find the next closed (not in use) port from some port range.

hi, i would like to create a bash script that check which port in my Linux server are closed (not in use) from a specific range, port range (3000-3010). the print output need to be only 1 port, and it will be nice if the output will be saved as a variable or in same file. my code is: ... (2 Replies)
Discussion started by: yossi
2 Replies

2. UNIX for Beginners Questions & Answers

Escape bash-special character in a bash string

Hi, I am new in bash scripting. In my work, I provide support to several users and when I connect to their computers I use the same admin and password, so I am trying to create a script that will only ask me for the IP address and then connect to the computer without having me to type the user... (5 Replies)
Discussion started by: arcoa05
5 Replies

3. Ubuntu

Convert a bash to ash

hello everybody, i'm a beginner in ash and i want to convert this bash script to ash. this script send a xml file to a nagios server : #!/bin/bash PROGNAME=$(basename $0) RELEASE="Revision 0.3" print_release() { echo "$RELEASE" } print_usage() { echo "" echo "$PROGNAME... (6 Replies)
Discussion started by: mdijoux25
6 Replies

4. Ubuntu

Bash to Ash, errors and adjustments

I wrote Bash script and now I want to convert it to Ash. One headache is this function: do_adduser() { setaddprompt _arr_add=("Add manually" "Add via TXT" "return to main menu" "exit program") select add_action in "${_arr_add}" do case "$REPLY" in 1)... (7 Replies)
Discussion started by: fzivkovi
7 Replies

5. Shell Programming and Scripting

Bash Script to Ash (busybox) - Beginner

Hi All, I have a script that I wrote on a bash shell, I use it to sort files from a directory into various other directories. I have an variable set, which is an array of strings, I then check each file against the array and if it is in there the script sorts it into the correct folder. But... (5 Replies)
Discussion started by: sgtbobie
5 Replies

6. Shell Programming and Scripting

Ash shell character corrupted after gdbserver

I am trying to debug something using gdbserver, after the successful/YET REALLY SLOW debug session i see that the ash shell on the the target under debug is messed up. Probably because gdbserver tries to open the core file which is binary. How do i recover from it? Also any ideas to speed up... (2 Replies)
Discussion started by: dragonpoint
2 Replies

7. UNIX for Dummies Questions & Answers

Differences in BASH and ASH shells regarding if command?

Guys I now have a script that's working in a BASH environment, however one line doesn't appear to be working on an embedded device that has a busybox therefore ASH shell. I've googled but there's very little I can find regarding the ASH shell. In BASH the following line works... if ] ;... (6 Replies)
Discussion started by: Bashingaway
6 Replies

8. Shell Programming and Scripting

Why generate "ash and bash" different output for same bash script?

Hi, For my bash script, terminal with bash is generate an OK output and program works right. already, terminal with ash have "line 48: syntax error: Bad substitution" output and program don't work. :confused: (0 Replies)
Discussion started by: s. murat
0 Replies

9. UNIX for Dummies Questions & Answers

Matching character

Alright, I am stuck here. I have this variable that stores the word = HELLO and I have converted it it to ----- I have asked user to input one character at a time. SAy, if they enter E. Therefore, I need to search 2nd character and input E there. makes it -E--- (other checkings have been... (2 Replies)
Discussion started by: felixwhoals
2 Replies

10. UNIX for Dummies Questions & Answers

character matching

Is there a way to pull out a character at a time from a work in unix, using a shell script? (1 Reply)
Discussion started by: Rukshan
1 Replies
Login or Register to Ask a Question