LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-27-2013, 08:28 AM   #1
omkar.jadhav
LQ Newbie
 
Registered: Aug 2013
Posts: 11

Rep: Reputation: Disabled
checking whitespaces in a string


i want to create a shell script which will take string (for eg: om-23%)as a user input and identifies in case that string contains any spaces (begining/in between characters/end/combination of all of these) of the string
 
Old 08-27-2013, 08:30 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
yes, do you have a plan or a test version?
 
Old 08-27-2013, 08:36 AM   #3
omkar.jadhav
LQ Newbie
 
Registered: Aug 2013
Posts: 11

Original Poster
Rep: Reputation: Disabled
i have created and tried the below loops but they all failes to detect spaces present at the end of the given string(for eg : om-23%<space><space> )
1. if [ "$string" != "${string% *}" ]; then
echo "$string contains one or more spaces";
fi
2. if ( echo "$string" | grep -q ' ' ); then
echo 'string contains one or more spaces'
exit 1
fi
 
Old 08-27-2013, 08:41 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
actually those spaces are ignored, you can check:
Code:
read a; echo "XX${a}YY"
> a d f g h j k            <-- spaces at the end
result:
XXa d f g h j kYY
so grep will not be able to find those trailing whitespace(s).
 
  


Reply



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
[SOLVED] Bash: Checking existence of environment variable using a string dwhitney67 Programming 14 06-28-2013 12:27 PM
[SOLVED] Bash: Checking for lower case and upper case letters in a string fatalerror0x00 Programming 1 12-09-2012 02:17 AM
[SOLVED] checking a string bayanaa Linux - Newbie 5 05-18-2012 08:02 AM
SH Prg: Checking if a string exists in a file ignignokt Programming 6 12-14-2006 01:31 PM
handling whitespaces in bash guerilla fighta Linux - General 2 01-25-2003 04:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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