Bash : Check aphanumeric content in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash : Check aphanumeric content in variable
# 1  
Old 11-23-2009
Bash : Check aphanumeric content in variable

Hello everyone, I'm trying the best way to implement a check on a variable ... in particular I need to assess the content of characters [a-zA-Z] and numbers [0-9], I tried on various manuals bash scripting but I could not figure out how to do ... any help?
# 2  
Old 11-23-2009
# 3  
Old 11-23-2009
Code:
  VAR="$@"
  if [ -z "${VAR//[a-zA-Z0-9]}" ]; then
    echo "VAR has only a to z, A to Z and 0 to 9!"
  else
    echo "VAR has non-alpha-numeric!"
  fi


./Test abc
VAR has only a to z, A to Z and 0 to 9!

./Test 123abc
VAR has only a to z, A to Z and 0 to 9!

./Test 123+abc
VAR has non-alpha-numeric!

./Test !abc
VAR has non-alpha-numeric!

./Test 123!
VAR has non-alpha-numeric!

# 4  
Old 11-24-2009
excuse me...
I think I have solved this way:

Code:
while ! [ -z "${VMC_NAME//[a-zA-Z0-9]}" ] || [ -z $VMC_NAME ]
do 
    echo ""
    echo "Error! Enter a valid value or only alphanumeric characters:"
    echo ""
    read VMC_NAME
done

do you think? If I enter strings containing characters not Alphanumeric or blank left for me in heaven until I enter an appropriate value, right?

Last edited by ionral; 11-24-2009 at 07:40 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to send to perl the content of a bash variable as argument?

I want to send the content of a bash variable as a python argument for processing. The problem is that bash doesn't "expand" the content, just sends the expression. script.sh#!/bin/bash export RECEIVEDDATE=$(date +"%Y%m%d.%H%M%S") perl checkdate.py checkdate.pyimport datetime import os... (3 Replies)
Discussion started by: Tribe
3 Replies

2. Shell Programming and Scripting

Check content

I would like to write a shell script to do the following , would advise how to make it ? very thanks 1. Connect LDAP server ( Id : user , password : pass ) 2. check the field "user_account_create_date" in the LDAP server , the format of data in this field in LDAP is ABC20130922 ( 22 Sep ,... (3 Replies)
Discussion started by: ust3
3 Replies

3. Shell Programming and Scripting

Check content of file

Hi All, I m very new to unix...i jus want to chk the content of file. ma requirement is if file has a content then display it else dont display or something pls specify which loop shalli use either for or while?? (20 Replies)
Discussion started by: navsan
20 Replies

4. Shell Programming and Scripting

Adding Content to Variable (bash)

is this possible? its kind of like incrementing the value of a number in a variable. but in this case, instead of the value of the variable being a number, it's just contents/strings/characters/alpha-numeric etc. NOT a number. For instance: VAR=Tommy for all in $(blah blah) do ... (2 Replies)
Discussion started by: SkySmart
2 Replies

5. Shell Programming and Scripting

korn shell: check the content of a string of a variable

hello, i have a variable which should have following content : var="value1" or var="value2" or var="value2:*" # example: value2:22 how can i check : - if the content is ok (value1 / value2* ) - the two options of "value2" when content is example "value2:22" , i want to split... (3 Replies)
Discussion started by: bora99
3 Replies

6. Shell Programming and Scripting

Can i use grep to check variable content correctnes?

I need to know if is possible to use grep to check content of a local variable, for eg. i use read index and i want to check if the index i read is in correct form, how do i do that i tried with grep but i get errors all the time dont know how to make it work.. thanks! (3 Replies)
Discussion started by: Goroner
3 Replies

7. UNIX for Dummies Questions & Answers

check length content existence of variable

hi guys, im learning so be gentle... i'm wanting to write a script to read in a customer number. in order that the code is robust i want to check 1) the length of the value entered (4 characters) 2) that all characters entered are numeric between the values 1 to 3 3) that a value is... (1 Reply)
Discussion started by: skinnygav
1 Replies

8. Shell Programming and Scripting

To check the content of one file in another

Hi , I have a file called "X" . the content of X are X -- abc def and i have a file called "Y" , the content of Y are Y -- erty sdss s abc sfs def I need to check if the content of file X is contained in Y.Only unix (7 Replies)
Discussion started by: giri_luck
7 Replies

9. Shell Programming and Scripting

How to check if two variable are empty strings at once? (bash)

I need to check if $1 or $2 are empty before continuing but I don't know if bash has any logic of the sort. This is what I'm looking for - except that "and" doesn't seem to work. if and ;then ... Thank you! :D (4 Replies)
Discussion started by: ph0enix
4 Replies

10. Shell Programming and Scripting

[bash] Check if variable is set or blank

Hello guys. In my script, i have the following code: echo "The tarfile contains these directorys" tar -tf file.tar > tarlist.txt cat tarlist | awk -F/ '{print $1 "/" $2}' | nl echo "Enter path of the directory you want to extract or just press enter to extract everything: " read path... (1 Reply)
Discussion started by: noratx
1 Replies
Login or Register to Ask a Question