Verify the variable has "space" or not?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Verify the variable has "space" or not?
# 1  
Old 07-11-2012
Verify the variable has "space" or not?

How to check if a variable contains space in it??? or any other character??? using if condition..
# 2  
Old 07-11-2012
unless its empty it will hopefully contain characters... What do you mean by other?
# 3  
Old 07-11-2012
Code:
grep " " -c

# 4  
Old 07-11-2012
Quote:
Originally Posted by vbe
unless its empty it will hopefully contain characters... What do you mean by other?

a="ind ia"
b="india"

here, variabe a contains space, but b dont...
like this, i want to check that the variable contains i or not...
# 5  
Old 07-11-2012
Code:
a="ind ia"
b="india"
$ echo $a | grep " " -c
1
$ echo $b | grep " " -c
0

# 6  
Old 07-11-2012
Please post what Operating System you have and what Shell you use.

If you are validating a field, it is better to work from a list of valid characters.
What are the valid characters?


If you just want to count the characters, one trick is to delete everything except that character, then count what's left:

Code:
# Counting space characters
echo "abc def"|tr -cd " "|wc -c
1
echo "abcdef"|tr -cd " "|wc -c
0
# Counting "a" characters
echo "abc def"|tr -cd "a"|wc -c
1
echo "abcdef"|tr -cd "a"|wc -c
1


@pamu
The grep -c command counts lines not characters.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

if [ "variable" = "numerical-range" ]; then

been a while so i'm a bit rusty and need a little help. writing a script that needs to compare $EXECHOST(a number) against a numerical range and then set a value. below isn't working but should give you folks an idea of my goal: if ; then echo "This is a 32B machine, exiting..." if ;... (4 Replies)
Discussion started by: crimso
4 Replies

6. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

7. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

8. Solaris

"Error for Command: verify"

I have a raid 5 configuration in a A3500 rack, manager by raid manager. This morning I found this messages in a /vad/adm/messages": Sep 10 04:33:57 zion scsi: WARNING: /sbus@3,0/SUNW,socal@2,0/sf@1,0/ssd@w200200a0b80735d3,4 (ssd11): Sep 10 04:33:57 zion Error for Command: verify ... (1 Reply)
Discussion started by: bonovox
1 Replies

9. Shell Programming and Scripting

verify ftp status is "good"

Hello, From reading prior "threads", my script looks like this: #!/bin/ksh -x ftpresults=`ftp -nv $ftphost1 << EOB user $ftpuser $ftppwd put $ftp_file $ft put $ftp_file $ft1 bye EOB` ftp_ctr=`echo $ftpresults | grep "226 Transfer complete" | wc -l`... (4 Replies)
Discussion started by: blt123
4 Replies
Login or Register to Ask a Question