LinuxQuestions.org
Help answer threads with 0 replies.
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 07-20-2011, 12:19 PM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 49
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, 12:46 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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, 01:12 PM   #3
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 49
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, 02:05 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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, 02:52 PM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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, 03: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, 08:49 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 07:32 AM   #8
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
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



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 12: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 03:15 AM
Bash scripting new line interpretation lord-fu Programming 4 06-29-2007 04:34 PM
Bash scripting - add a character to a line. welby Programming 1 01-14-2004 10:09 AM
bash line wrapping gone awol acid_kewpie Linux - Software 10 05-14-2003 05:04 AM

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

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