LinuxQuestions.org
Review your favorite Linux distribution.
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 07-20-2011, 01:19 PM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 48
Bash scripting line wrapping best practices


I have a bash script which has long (more than 80 cols) statements like these:
Code:
.....
GNOME_PANEL_ROOT_SETTINGS="$BSP_FILES_LOC_FULL/gnome-panel/gnome-panel-root.entries"
.....
if [[ $BSP_TYPE == "essential" || $BSP_TYPE == "full" || $BSP_TYPE == "livecd" || $QSP_TYPE == "dummy" ]]
.....
echo "The argument 'dummy' is always used with second argument. Please provide an appropriate second argument."
I deliberately picked the above statements which shows three different types of scenario -- Variable declaration, IF statement and a echo statement.

Now, I know I can shorten the length of the above by choosing a different variable or reformatting the sentence but that this is not the point because there can always be a scenario where you have used everything that is ideal but your line still goes beyond 80 cols.

So the question: What is the best way to get the above lines under 80 cols?

I usually use gedit or nano for writing scripts.
 
Old 07-20-2011, 01:46 PM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Well I do not know of any hard and fast rule that says 80 has to be the limit, but escaping the end of the line should work well.
After that I guess it comes to personal preference. I would probably leave the variable assignment.
Code:
.....
GNOME_PANEL_ROOT_SETTINGS="$BSP_FILES_LOC_FULL/gnome-panel/gnome-panel-root.ent\
ries"
.....
if [[ $BSP_TYPE == "essential" ||    \
      $BSP_TYPE == "full"      ||    \
      $BSP_TYPE == "livecd"    ||    \
      $QSP_TYPE == "dummy"     ]]
.....
echo "The argument 'dummy' is always used with second argument. Please provide \
an appropriate second argument."
 
Old 07-20-2011, 02:12 PM   #3
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 48
Quote:
Originally Posted by grail View Post
Code:
if [[ $BSP_TYPE == "essential" ||    \
      $BSP_TYPE == "full"      ||    \
      $BSP_TYPE == "livecd"    ||    \
      $QSP_TYPE == "dummy"     ]]
What I noticed for the above long IF statement is that if I do it in this way:
Code:
if [[ $BSP_TYPE == "essential" ||
      $BSP_TYPE == "full"      ||
      $BSP_TYPE == "livecd"    ||
      $QSP_TYPE == "dummy"     ]]
i.e. just hit enter key after every variable checking, the script works fine too. What is the difference between the one that I just showed above (and what is it called as?) and the one that you mentioned (escaping the new line with "\").
 
Old 07-20-2011, 03:05 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Nothing is really different that I am aware of ... it was more to keep it looking the same so you knew if you ever see '\' at the end of the line then the information is continued onto the
next line ... again a personal preference.
 
Old 07-20-2011, 03:52 PM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
I always use here docs and cat for printing long messages,
you can even interpolate commands and variables:

Code:
# redirect to stderr
1>&2 cat <<EOT

oops! failed on $(date)
it didn't work!

path is ${PATH}


EOT
 
Old 07-20-2011, 04:06 PM   #6
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112
Quote:
Originally Posted by kushalkoolwal View Post
What I noticed for the above long IF statement is that if I do it in this way:
Code:
if [[ $BSP_TYPE == "essential" ||
      $BSP_TYPE == "full"      ||
      $BSP_TYPE == "livecd"    ||
      $QSP_TYPE == "dummy"     ]]
i.e. just hit enter key after every variable checking, the script works fine too. What is the difference between the one that I just showed above (and what is it called as?) and the one that you mentioned (escaping the new line with "\").
The short answer is, shell interpreters basically ignore white space, which allows us to code our scripts in a human-friendly fashion. So if you have consecutive spaces (all over the place in both examples), it treats them as one space, and if you have a newline character within a shell construct (the if construct, in this case) then it'll ignore them until it finds the end of the construct.

It does this because the computer doesn't care about aligning columns, but it makes things easier for us.
 
Old 07-20-2011, 09:49 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
One trick with echo if you like it
Code:
echo -n "The argument 'dummy' is always used with second argument."
echo " Please provide an appropriate second argument."
http://linux.die.net/man/1/echo

I generally agree with leaving the var assignment as is, although it may make your code more flexible to assign separate vars to dirs and filenames and concatenate as reqd.
 
Old 07-21-2011, 08:32 AM   #8
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,320

Rep: Reputation: 252Reputation: 252Reputation: 252
I think there is a difference whether you use \ or not (not only personal taste). In the above statement you have the line breaks at a boundary of a statement, but not inside a statement (I think the if construct might not be finished, but the if on its own is finished while the comparison below isnít finished). So:
Code:
#!/bin/bash

if [[ $BSP_TYPE == \
       "essential" ||
      $BSP_TYPE == "full"      ||
      $BSP_TYPE == "livecd"    ||
      $QSP_TYPE == "dummy"     ]]; then
    echo yes
fi
will need the \ in the first line.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Line wrapping broken on Bash 3.0 ninjabob7 Linux - Software 4 09-21-2009 01:50 PM
need to delete a line if a field of that line matches using awf in bash scripting accesskarthi Linux - Newbie 8 06-29-2009 04:15 AM
Bash scripting new line interpretation lord-fu Programming 4 06-29-2007 05:34 PM
Bash scripting - add a character to a line. welby Programming 1 01-14-2004 11:09 AM
bash line wrapping gone awol acid_kewpie Linux - Software 10 05-14-2003 06:04 AM


All times are GMT -5. The time now is 03:19 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration