LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-13-2011, 10:18 AM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Rep: Reputation: 60
Detecting Whitespaces in Shell Scripts (Bash,Korn etc.)


I cring when I say this but when using notepad in windows, if you have content in the file such as a list of ip address and you hit "control a" on your keyboard, it will show the whitespaces in blue. Is there a way in linux to detect unnecessary white spaces in a file that could cause issues when creating shell scripts? I ran into this before from a previous post:


http://www.linuxquestions.org/questions/programming-9/expect-function-help-using-bash-906440/



Any tricks?

Regards

Last edited by metallica1973; 10-13-2011 at 10:25 AM.
 
Old 10-13-2011, 10:39 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Here's a bunch of ways to do that in vim: http://vim.wikia.com/wiki/Highlight_unwanted_spaces.

Hope this helps some.
 
Old 10-13-2011, 10:51 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
[EDIT]Apparently I did read the OP wrong so the text below doesn't apply, sorry.[/EDIT]

in BASH you could for instance:
Code:
 ~]$ whitespace() { [ "${1}" = "${1// /}" ] || echo "Got whitespace"; }
 ~]$ whitespace "RIP Dennis Ritchie"
Got whitespace
or

Code:
 ~]$ hasSpace() { A="$1"; B=(${A}); [ ${#B} -eq ${#A} ] || echo "Got whitespace"; }
 ~]$ hasSpace "RIP Dennis Ritchie"
Got whitespace
or

Code:
 ~]$ A="RIP Dennis Ritchie"; IFS=' '; set -- $A; echo $(( $#-1 ))
2
or really lame

Code:
 ~]$ hasSpace() { echo "$1"|grep -q "[[:blank:]]" && echo "Got whitespace"; }
 ~]$ hasSpace "RIP Dennis Ritchie"
Got whitespace

Last edited by unSpawn; 10-13-2011 at 12:00 PM. Reason: //Explanation
 
Old 10-13-2011, 10:57 AM   #4
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by metallica1973 View Post
"control a" on your keyboard
Ctrl-a is the Windows shortcut for select all (not just spaces). It works in most Windows applications.

On Linux, Keyboard shortcuts may do different things, or nothing at all, in different applications. Ctrl-a seems to work pretty much the same in Kate on Kubuntu as it does on Windows. In Emacs it moves the cursor to the beginning of the current line (ie the most recent character directly preceded by a newline). Different distros may do things differently too, so YMMV.

Without knowing anything about the application you're using to compose your shell scripts it is a little impossible to guess how you are getting extra spaces, or how to detect them. If you really like Microsoft's Notepad, you might look into using something like Notepad++.

HTH
 
Old 10-13-2011, 11:38 AM   #5
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
Awesome,

thanks
 
Old 10-13-2011, 12:07 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
I would also question the use of ctrl-a. It will highlight all text and has nothing to do with white space at all in Windows.
 
1 members found this post helpful.
Old 10-14-2011, 08:56 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Extra whitespace around and after commands in shell scripts isn't generally a problem anyway. Your shell just ignores it. They normally only cause errors inside certain syntactical elements, such as with the infamous [ command. Anyone with experience can easily spot these problem spaces.

The only place I can think of where a space would cause problems, and not be easily noticed, is when you use backslashes to split a command onto multiple lines. e.g.:

Code:
this is a very long command line with many arguments and so I am breaking \
it up onto two lines with a backslash.
The backslash escapes the non-printing newline character at the end of the line, so that the shell treats it as normal whitespace (i.e. it ignores it) instead of a command terminator. If you had a space character after it, then it would escape that instead, and the newline itself would still be "live".


A bigger problem is if you generate a script or input text file using Notepad or a similar Windows program, and then try to run it on a *nix box. Since Windows uses dos crlf line-endings, there will be extra non-printing carriage-return characters at the end of each line, which will likely cause syntax errors.


Edit: I just checked out the thread above, and am reminded that the terminating block of a here document is another potential pitfall location.

Also, another way to see non-printing characters in a file is to run it through cat -A. It doesn't highlight spaces directly in any way, but it does highlight newlines, so there will be a gap between the final alphabetic character and the end of the line, if any exist there.

Last edited by David the H.; 10-14-2011 at 09:05 AM. Reason: as stated
 
1 members found this post helpful.
  


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
Diffrence between Korn and bash shell in Linux msivasakthi Linux - General 4 04-13-2011 12:52 AM
Bash, LS, For loops, and whitespaces in directories jhrbek Programming 27 09-22-2010 05:17 AM
Bash script - escaping whitespaces colabus Linux - Newbie 15 04-29-2006 11:58 PM
Using a Korn shell in rc scripts desbyleo Solaris / OpenSolaris 3 11-12-2004 05:27 PM
handling whitespaces in bash guerilla fighta Linux - General 2 01-25-2003 04:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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