![]() |
Global variables in shell scripts
I am not sure how to word this. Let's say I write a lot of shell scripts for my Macs at work and I want to set up an array of global variables that any script can call, outside of the script. Eample:
$1 = current user $2 = name of start up volume $3 = computer name $4 = current IP address of the NIC You get the point? How would one go about accomplishing such a thing? Run all scripts through a framework? #Edit# I guess I could maybe alter the profiles for every user for bash and set functions and aliases and variables in the bash profile and then whenever that user runs a script, they could call those variables.... |
The easiest (and probably best) way is to define those variables in a script that is then 'source'd by the other scripts.
globalVariables.sh Code:
#!/bin/bashCode:
#!/bin/bash |
Hayne,
As always you are right on the money. My top google hits pretty much returned what you posted. Thanks as always. |
Don't use bash for such simple tasks. Use the 'standard' bourne shell (/bin/sh).
Current user is already defined with $USER. So is the computername; $HOST. |
Quote:
Also, sometimes a script will run as root, so I need to figure out what the current user is. My method is seeing who currently owns the console, since the currently logged in user should always own the console. Code:
ls -l /dev/console | awk '{ print $3 }' |
Quote:
(I'm assuming that the scripts are for OS X only, so availability of 'bash' (e.g. on older hardware) is not an issue.) By the way, note that on OS X, 'sh' is just 'bash' invoked under another name. |
Quote:
Quote:
There's no notion of "current user". Unix is a multi-user system and so is OS-X. There can be multiple users logged in at the same time. |
Quote:
See my comment above about 'bash' and 'sh' being essentially the same program on OS X. Quote:
|
Quote:
Still, I would use the bourne syntax. I've seen to many scripts riddled with bashisms that people try to run on systems that only have the 'regular' bourne shell. It's easier to learn new things then it is to unlearn bad habbits :D |
Quote:
But nowadays I don't think there are many people who care about any Unix system other than OS X and Linux. |
I have BSD 8, Ubuntu 9, Fedora 10, OS X, and open SuSe at home. All of them by default use /bin/bash as the default shell. I just recently bought a few books on bash, and the books all tell me, that bash has full functionality of shell, and in most cases works both ways. With some exceptions where bash is more functional than bourne shell.
I looked around, and the only other shell I can see that is close to being as widely used as bash seems to be korn. I don't know all the differences of the shell because I haven't extensively used them all. In the current O'Reilly book I am reading, which is all about bash 3.0. The first chapter states that bash takes all the good things from c shell, korn, and bourne shell, and combines them with other robust features to create bash. Also, there are many global variables I could use while scripting. If I want to invoke say the defaults write command on a script to modify a property list, I need to do so as the user, not as root, because that will just modify the root user. So, I could hard code these global variables to get a list of all user accounts, and then loop it to modify that plist. That is just one small tiny example. Not to mention, this is my enterprise, these are my organizations systems with the image I created. So, setting up a series of global variables may be very beneficial for me from a systems management view. Also, everything is standard, so I know what is going on and I know by default every shell is set to bash. Thanks for your time guys, it is always appreciated. |
Quote:
Quote:
|
Quote:
|
Quote:
So the notion that folks 'should' prefer sh is false... or, only true if we want our scripts to run on machines which have no bash binary. (safe to say: Mac users need not have that care). sh is an ancient relic too old to be concerned about, and bash offers too many improvements to ignore. |
Quote:
And you're completely missing the point. But that seems to happen a lot on this board :rolleyes: |
Quote:
Quote:
I write ~80% of my maintenance scripts in PHP anyway. Quote:
Quote:
Quote:
Quote:
The original question was for OSX scripts, and bash is omnipresent, so no problem. |
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
|
Quote:
Quote:
;) Seriously though: over the years, Bash has borrowed little bits here and there from zsh (and Perl?), and those enhancements are becoming (have become?) the 'norm' now. Bash *is* the new sh. I don't think anyone here is worried about running their scripts on a PDP-11. :D |
Sorry i didn't catch this last night (perhaps since it's part of a reply to someone else, i just skipped it before):
Quote:
Two things about that...
|
well back on topic....
So, when I set up my commands, and then source it, it does not seem to be working. What is the best method? Can I literally just put 1=`some commands` and then echo $1 or call for $1 in a script sourced to my global variables? Thanks So for example, I am looking at using functions, but not sure how to output them to numbered variables... Code:
#!/bin/bash |
Quote:
Quote:
|
Well, certain things get parsed at log in with hard coded variables in OS X, so when a script runs as a log in hook, $1 is always something, $2 is always something, so I was assuming that login hooks sourced some commands which output the desired result to $1.
So, in my case, if I sourced my global variables, and say the currently logged in user is output to $1 I could do this in a script defaults write /Users/$1/Library/Preferences/com.mycompany "some_options" -bool true I think I want to build functions, but just not sure how to set them to certain variables. I know some third party stuff also uses $1, $2, $3 at log in to utilize certain hard coded variables. Sorry if I am not making much sense |
Third party stuff does not set $1, $2, etc to values - as I said before, those values come from the command-line args used when the script was invoked.
I'm not sure what you are missing - i.e. why the sort of example (with suggested variable names) I gave above is not what you want. |
Quote:
I guess, I don't have to output it to a number, but I was thinking it may be easiest that way. Thanks |
Quote:
|
Quote:
|
Quote:
From post #2: Quote:
Quote:
# SHELL SCRIPT: $ cat foo Code:
# CONFIG FILE: $ cat .myConfigs Code:
And, here is what it looks like when it's run: Code:
BTW... Quote:
|
I was thinking I could pass a bunch of arguments to a function and then just call that function in a script. I could, case things for different OS versions and what not. Though, this is getting slightly above my skill level, but I am willing to learn.
I just think numbers would be easier than word variables, but then again as long as I am writing everything I can kind of do it how I want. I was thinking more along the lines of setting this up, so when other people in my department need to write a script they can call global variables for simple one liners instead of hashing everything out. Thanks |
Still using my foo script from post #27, perhaps this run (including some args on the command line this time) will better illustrate what's happening:
Code:
$ foo these are arguments |
| All times are GMT -5. The time now is 05:51 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.