test for a blank string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting test for a blank string
# 1  
Old 02-08-2012
test for a blank string

Hi guys
I am performing a simple test for a blank string with the following code:
Code:
   if [[ ! ${user_input} =~ [^[:space:]] ]] ; then
        echo "Blanks are NOT a valid input "
        return 1
   fi

The above fails giving a syntax error message:
Code:
syntax error at line 142 : `=~' unexpected

I am in ksh88

is there any other way to test for a blank string. (it should have blanks and/or tabs to be blank) ?
or maybe someone can tell me what am I doing wrong in the code above. Thanks a lot for advice -A
# 2  
Old 02-08-2012
The test above =~ is pure Perl, you need to use either Perl the grep command to perform your regex chack as regex is not available in the shell.
thus
Code:
$ VALUE=
$ echo $VALUE | grep -E '^[:space]*$'

$ echo $?
0
$  VALUE=1
$ echo $VALUE | grep -E '^[:space]*$'
$ echo $?
1

OR
Code:
$ echo $VALUE
1
$ perl -e 'print "BAD_USER\n" if $ARGV[0] =~/^\s*$/' $VALUE
$ VALUE=
$ echo $VALUE | perl -e 'print "BAD_USER\n" if/^\s*$/'
BAD_USER

# 3  
Old 02-08-2012
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a string instead of blank lines

how can i insert a string sush as "###" instead of blank lines in a file? i try this code but it doesn't work! awk 'NF<1 {$1=="###" ; print$0}' in_file > out_file (13 Replies)
Discussion started by: oreka18
13 Replies

2. Shell Programming and Scripting

Inserting blank lines after string change

My input data looks like this ... -150 120 8 -150 122 7 -150 124 11 -150 126 8 -150 128 19 -150 130 13 -150 132 26 -150 134 38 -150 136 45 -150 138 62 -150 140 75 -150 142 110 -150 144 139 -150 146 138 -150 148 158 -150 150 173 -150 152 217 (5 Replies)
Discussion started by: chrisjorg
5 Replies

3. UNIX for Dummies Questions & Answers

Creating a blank string of a specified size

I want to have a string which has n blank spaces For example set N = 3 create str = " " So the length depends on the value of N. I am in tcsh. (4 Replies)
Discussion started by: kristinu
4 Replies

4. Shell Programming and Scripting

String starting with blank space

Dear Masters, Need your help for this small query. I am trying to get all the strings that starts with a blank space as output. Limitation is , it should be done only through AWK. Input will be: aa _1bbb c-ccc ddd eeeee ff Output should be: _1bbb c-ccc eeee ff (5 Replies)
Discussion started by: patric2326
5 Replies

5. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

6. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

7. UNIX for Dummies Questions & Answers

selective removal of blank spaces in string

Hi, I'm a newbie to shell scripting and I have the following problem: I need all spaces between two letters or a letter and a number exchanged for an underscore, but all spaces between a letter and other characters need to remain. Searching forums didn't help... One example for clarity: ... (3 Replies)
Discussion started by: Cpt_Cell
3 Replies

8. Shell Programming and Scripting

Find a string and place two blank lines

Hi friends, I am looking for a line to find a particular string in my file and once found then replace with 2-3 blank lines before the string Example: aaa 11 bbb 1 2 3 aaa 22 bbb 4 5 6 Output (4 Replies)
Discussion started by: shaliniyadav
4 Replies

9. UNIX for Dummies Questions & Answers

string with blank spaces

I have a file that has dates like this: date FINAL_RESULT; 7 date FINAL_RESULT; 2 date FINAL_RESULT; 5 With this command: seira=`cut -f2 -d\; tes.txt` i take the date FINAL RESULTs and i store them on variable seira.then seira look like this: 6 3 8 I want to read seira and make a sum of all... (4 Replies)
Discussion started by: psalas
4 Replies

10. Shell Programming and Scripting

remove blank spaces in a string

can any help how to remove blank spaces in a string? STR="GOOD BYE" by removing blank spaces, the string should be GOOD,BYE thanks in advance (2 Replies)
Discussion started by: spandu
2 Replies
Login or Register to Ask a Question