LinuxQuestions.org
Review your favorite Linux distribution.
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 11-18-2015, 11:20 PM   #1
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311
Blog Entries: 2

Rep: Reputation: 16
Unhappy PATH Variables: Ergonomic Assignment Dilemma


To "/" or not to "/".

I have a dilemma over assigning variables for the following shellscript backup program:

Code:
#!/bin/bash


#VARIABLES
	#Initialize
		unset BOOT DISK0 DISK1 DISK2 SOURCE TARGET1 TARGET2 USER
	
	#Define
		#Individual
			DISK0=/Sea_ext4
			DISK1=/Sea_ntfs
			DISK2=/Sam_HM320II
			USER=/$(echo $(whoami))

		#Composite
			SOURCE=/media${USER}${DISK0}/recent
			TARGET1=/media${USER}${DISK1}/recent
			TARGET2=/media${USER}${DISK2}/recent
		

#CORE DATA
	#Synchronization
		rsync -Savi -Eu -stat --delete ${SOURCE}/AC ${TARGET1};
		rsync -Savi -Eu -stat --delete ${SOURCE}/AC ${TARGET2};
When assigning the variables above, should I include the "/", e.g.
Code:
	DISK1=/Sea_ntfs
	DISK2=/Sam_HM320II
or not, e.g.
Code:
	DISK1=Sea_ntfs
	DISK2=Sam_HM320II
This may seem like an extremely small insignificant matter, but when you include the "/" in the pathname, than
1) in the next section Composite Variables, than you have to look at a morass of several variables lumped together, "/" included; e.g.
Code:
		SOURCE=/media${USER}${DISK0}/recent
		TARGET1=/media${USER}${DISK1}/recent
rather than "/" excluded; e.g.
Code:
		SOURCE=/media/${USER}/${DISK0}/recent
		TARGET1=/media/${USER}/${DISK1}/recent
2) in the main code, you have pathnames starting out without any "/", e.g.
Code:
	rsync -Savi -Eu -stat --delete ${SOURCE}/AC ${TARGET1};
rather than "/" excluded from the individual path name variables; e.g.
Code:
	rsync -Savi -Eu -stat --delete /${SOURCE}/AC /${TARGET1};
With the colorized themes that most IDEs provide, seeing a different colored "/"
1) between the two variables
2) before pathnames
can make the code more clear, and can make the usual boring morass become alive.

Is there some programming board/standard (e.g. POSIX) that advises programmers of a standard of whether or not to include the "/" in the Individual Variable names?

Last edited by andrew.comly; 11-19-2015 at 09:57 PM. Reason: indent
 
Old 11-19-2015, 02:40 AM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Dunno if there is any standard but I say its more preference or liking that any thing much else.
As for compatibility reasons I would never use absolute path names only relatives. And if you have to use absolute path try to use a variable for this. And within the actual script only have relative ones. Something like

Quote:
root="/var/www"
logs="logs"
upload="upload"
mv "$root/$upload" "$root/$upload/old"
This way you can move the script to another server and just have to adjust root variable. Also you still have "morass"...
I think BASH is bad for reading these kind of things. But php for example is a bit nicer and more clearly to read
Quote:
$root="/var/www"
$logs="logs"
$upload="upload"
cmd="mv $root .'/'. $upload $root .'/'. $upload .'old'
The main reason why I would never use / at the start of a path is that / is the root on linux systems. This way if you forget to set any path variable before this you end up starting with / for your path. This can be troublesome and might lead to some exploits even.
 
1 members found this post helpful.
Old 11-19-2015, 03:23 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
The name of the variable should describe the content (except for vairable 'USER': it is usually preset and no to be played with):

Code:
DISK_LABEL=DORINA
DISK_PATH="/dev/disk/by-label/$DISK_LABEL"
 
1 members found this post helpful.
  


Reply

Tags
bash, ergonomic, paths, shell scripting, variables



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
oustion about variables(PATH variables) mehrd@d Linux - General 2 08-04-2013 03:32 AM
setting the path variables sudheer.svss Linux - Newbie 3 02-19-2013 08:10 AM
How to set PATH variables vivekr Linux - Newbie 10 06-04-2012 07:35 PM
adding variables to the PATH myrto Linux - Newbie 8 01-23-2007 08:34 PM
Setting PATH and other variables durrantj Ubuntu 2 08-25-2005 07:44 PM

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

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