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 12-16-2016, 03:15 PM   #1
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
Shell coding style?


Again and again I am confronted with this wide-spread coding style
Instead of
Code:
#!/bin/bash
export PATH=/bin:/usr/bin:/sbin:/usr/sbin

ifconfig -a | grep "inet" | sed 's#.*inet[^:]*: *\([a-z0-9.:/]\{1,\}\).*#\1#'
I see
Code:
#!/bin/bash
IFCONFIG=/sbin/ifconfig
GREP=/bin/grep
SED=/usr/bin/sed
CUT=/usr/bin/cut

$IFCONFIG -a | $GREP "inet" | $SED 's#.*inet[^:]*: *\([a-z0-9.:/]\{1,\}\).*#\1#'
Who teaches this?
Isn't this harder to type & harder to read & harder to port to another OS?
Another observation: these variables are ALWAYS UPPERCASE. I have never seen them lowercase.
So maybe it is from an old book, from the CP/M era?
Most useless seems
Code:
IFCONFIG=`which ifconfig`
GREP=`which grep`
SED=`which grep`
Why?
Is searching the PATH for each command too resource intensive?
Doesn't the shell have a hash table?

Last edited by MadeInGermany; 12-16-2016 at 03:19 PM.
 
Old 12-16-2016, 03:58 PM   #2
Myk267
Member
 
Registered: Apr 2012
Location: California
Posts: 422
Blog Entries: 16

Rep: Reputation: Disabled
Where is this widely spread? That looks a little ridiculous to me and unlike anything I've ever seen.

It's almost like the person writing it expects what they're doing to be more portable, by, uhh, hardcoding the paths in. Okay, maybe not. My only other guess is that they saw some distant day when they would, perhaps, have a better version of `cut` to sub in? Who knows.

Though, uppercase globals isn't too crazy. Lots of people do that in all sorts of languages. I do it, too.

Edit: Well, I guess I don't know what I know anymore. In the other thread szboardstretcher posted a link to a bunch of their shell functions and they do indeed a similar style to the OP: https://paste.ubuntu.com/23639585/ So maybe there's a good reason for it.

Last edited by Myk267; 12-16-2016 at 04:02 PM.
 
Old 12-16-2016, 04:19 PM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Original Poster
Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
I don't argue with using uppercase shell variables in general.
While I still follow the UNIX convention
  • environment variables = uppercase
  • shell variables = lowercase
 
Old 12-16-2016, 05:02 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
I googled this because my reaction was lolwut?

The only result I got was this one:

http://stackoverflow.com/a/795540

If that poster is to believed, then the pattern is actually supposed to be:

Code:
# Definition of $GREP is here
source commands.sh

$GREP whatever
Where commands.sh might define $GREP as "grep -E" or "egrep" or whatever, depending on the system you're on, with the point being to get consistent behavior across different Unixes.

Last edited by dugan; 12-16-2016 at 05:04 PM.
 
Old 12-16-2016, 07:07 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
a little reading goes a long way.

3.2. Variables

http://linuxcommand.org/

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

Last edited by BW-userx; 12-16-2016 at 07:31 PM.
 
Old 12-16-2016, 08:22 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Perhaps it's following typical Makefile style?

Code:
CC = gcc
objects : sources
    $(CC) -o ...
 
Old 12-17-2016, 04:49 AM   #7
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 can probably follow the use if it were for something like cron where often the full path is required to know where a command is.

i have also played with my own package management system which did utilise the sourcing of files, as per dugan's example, to have executables with the switches needed for each command

I am also a +1 to the variable format listed by MadeInGermany
 
Old 12-17-2016, 08:00 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
specifying the full path is ok, that has some benefits:
in a production environment (where several people do the same build) PATH may contain local, user specific settings, but the build process itself will use the hardcoded "default" path. It is safer, more secure, and probably a bit faster too (you can also specify default flags). By using these variables you will be able to switch/replace a command with a similar, other one without knowing where it is really used.

For a personal, single user environment it is not really important.

to the variable format: using sed (as real command) and $sed (as a variable) too can be confusing and hard to recognize the difference (a missing $). That's why upcase can be suggested. Constructing new functions may not lead to such confusion, therefore lowercase function names are ok.
 
1 members found this post helpful.
Old 12-17-2016, 01:25 PM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Original Poster
Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
And who told you to use /path/to/cmd instead of PATH= and cmd?
Both eliminate individual user PATHs.
 
1 members found this post helpful.
Old 12-17-2016, 05:08 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
There are cases where commands in variables make sense within the immediate scope, but as a kind of default environment setting method/style I find it much less satisfying.

Quote:
Originally Posted by pan64 View Post
By using these variables you will be able to switch/replace a command with a similar, other one without knowing where it is really used.
...and thus potentially break many more downstream uses in the process than you will ever know, what fun!

That works in make files (build environment), but I think it is much less desirable in shell script code (executable environment), especially when you don't know where/how/by-whom it may really be used!

I agree with others that if the actual point is to enforce a default PATH, then set the PATH environment variable in the desired scope.

If the point is to set command options for use by as yet unknown or to-be-written future scripts, that leaves me with an uneasy feeling. Leave the optional behavior choices to the script writers who have full awareness of the use case at the time of writing, and who may rely on the actual default behaviors of the commands.

Quote:
Originally Posted by pan64 View Post
to the variable format: using sed (as real command) and $sed (as a variable) too can be confusing and hard to recognize the difference (a missing $). That's why upcase can be suggested. Constructing new functions may not lead to such confusion, therefore lowercase function names are ok.
Agreed. In any case that you must define a command in a variable of same name, upper-case it if only for this reason! But I think I would never define a command variable of same name outside possibly a make file!

Code:
sed=/usr/bin/sed
SED=/usr/bin/sed

PATH=/bin:/usr/bin:/sbin:/usr/sbin
MY_SED="sed -r"

Last edited by astrogeek; 12-17-2016 at 05:41 PM. Reason: method/style
 
Old 12-18-2016, 02:51 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
Quote:
Originally Posted by MadeInGermany View Post
And who told you to use /path/to/cmd instead of PATH= and cmd?
Both eliminate individual user PATHs.
using explicit path (= using full/absolute path) sometimes faster, especially when PATH is too long. And also you can use commands "outside" of your PATH without configuration, without increasing your PATH.
Also you may have several tools with the same name (two or more different python/perl/make/whatever available), and probably you want to use the specified version from a given directory. Relying on PATH probably won't work as expected.
Based on my own practice: using well defined commands may avoid some very strange, tricky and unpredictable errors.
 
Old 12-23-2016, 07:29 PM   #12
budman85
LQ Newbie
 
Registered: Sep 2005
Location: Montreal Canada
Distribution: Slackware
Posts: 23

Rep: Reputation: 0
I often see uppercase names to clarify, it's local to the script.
This was used in KSH and Bourne as well.
It's so the variable never gets interpreted as a system command.

As for the hard coded path. You may never see it on your own local PC, however,
when you are working in a large system environment, directories are often symlinked to hell
due to network routing. Explicit paths can make a script fly, as having to resolve its path every single time.

Often times, the latest bins are in a disted share, these may be specialized (bastardized) versions of the utilities customized for internal use (kerberized). These are often a restricted form of the command. The joys of working with SoD (Separation of Duties) where NO Dev shall ever touch a PROD space ever again, without applying for Permission from Production Operations.

Once case, when we migrated from RHEL4 to RHEL6, we found KSH had been update SO much in RHEL6, it broke hundreds of scripts. Some opted to use the KSH93 link, and others opted to fix the scripts to use KSH2010.

Another example, the lftp or ssh commands being updated to work with Kerberos libraries. The /bin/ssh won't work on machines, because it will no longer negotiate. So we are forced to hardcode to the disted version in order for the scripts to work properly.

These are cases where `which ssh` would fail miserably.

In the real world, which plays nice an works most of the time. Unless you want or need to use special versions, then the only method is hardcoding to ensure you are using the correct one.



Regards
Rich
 
Old 12-24-2016, 04:01 AM   #13
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622
Blog Entries: 40

Rep: Reputation: Disabled
If a coding style is not a convention between several people, it has no right of existence. Otherwise, two distinct groups do as they please or discuss a consensus to help organize a joined programming effort.

*I* got rid of many dumb restrictions by quitting professional software-development. Now *I* enjoy my 1 and only coding style which may adapt to the language currently used, or will just be the same with any of them. *I* use upper-case for constants (and do not care about bash, nor the environment), i.e. variables that *I* consider constant.

Should I ever have to hand my code to someone else, we can discuss again. But the presupposition of teamwork or development in communities must be pronounced before a discussion of coding-styles makes any sense. The way it is imposed on us from time to time is not even constructive. Maybe I live in France for too long, already, but then say so. In German, we do not have two words for Liberty and Freedom, which is a handicap sometimes and has, probably, contributed to the way that coding-styles are advocated (not “applied”), nowadays.

Frohe Weihnachten, Ihr Nasen.

Last edited by Michael Uplawski; 12-24-2016 at 04:03 AM.
 
  


Reply

Tags
coding, programming, shell



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
Coding on border in re color style satimis Linux - General 4 08-24-2014 09:42 PM
[SOLVED] Coding Style checking zinat Programming 14 05-05-2014 01:58 PM
GTK coding style MTK358 Programming 7 05-21-2010 08:27 PM
Preferred coding style? MrCode Programming 26 03-27-2010 02:34 PM
gnu style coding...Do u agree? alaios Programming 18 09-01-2005 07:28 AM

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

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