The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   Global variables in shell scripts (http://hintsforums.macworld.com/showthread.php?t=110127)

hayne 03-18-2010 08:38 PM

Quote:

Originally Posted by tlarkin (Post 576515)
So, when I set up my commands, and then source it, it does not seem to be working.

As usual when asking about a problem, you need to supply details of what happens and why this is not what you want.

Quote:

Can I literally just put 1=`some commands` and then echo $1 or call for $1 in a script sourced to my global variables?
I don't think "1" is an allowable variable name. $1, $2, etc are special variables that get set to the command-line arguments of a script.

tlarkin 03-18-2010 09:12 PM

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

hayne 03-18-2010 09:52 PM

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.

tlarkin 03-18-2010 10:05 PM

Quote:

Originally Posted by hayne (Post 576534)
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.

Well, in the specific example I gave, parameters do get passed to $1, $2, and $3 by third party framework, but only at login. I assume that is because they modified the existing Apple stuff when running scripts as log in hooks.

I guess, I don't have to output it to a number, but I was thinking it may be easiest that way.

Thanks

hayne 03-18-2010 10:18 PM

Quote:

Originally Posted by tlarkin (Post 576537)
Well, in the specific example I gave, parameters do get passed to $1, $2, and $3 by third party framework

Your use of the word "framework" makes me think that you are talking about shell scripts invoked from a compiled program (e.g. one written in C). In that case, the compiled program specifies what command-line arguments it wants the shell script to receive. And then Bash sets $1, $2, etc to those command-line arguments.

tlarkin 03-18-2010 10:52 PM

Quote:

Originally Posted by hayne (Post 576538)
Your use of the word "framework" makes me think that you are talking about shell scripts invoked from a compiled program (e.g. one written in C). In that case, the compiled program specifies what command-line arguments it wants the shell script to receive. And then Bash sets $1, $2, etc to those command-line arguments.

That very well could be what it is

Hal Itosis 03-19-2010 02:55 AM

Quote:

Originally Posted by tlarkin (Post 576515)
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

It sure seemed that post#2 (hayne) covered it pretty well. Observe how "numbers" (positional parameters) are not connected to "global variables" through any special relationship.

From post #2:
Quote:

otherScript.sh

Code:
#!/bin/bash
source /path/to/my/scripts/globalVars.sh
echo "Computer name is $computerName"
Note how otherScript.sh echos some variable called "computerName" seemingly out of nowhere? It didn't declare or initialize it anywhere. Where did it come from? Did you test out his two files to understand how they work?


Quote:

Originally Posted by tlarkin (Post 576515)
So for example, I am looking at using functions, but not sure how to output them to numbered variables...

<snip>

I just can't quite wrap my head to output that code so that it equals $1, and then when I source it in a script I can just use $1 and it will always pull the currently logged in user.

If you want "numbered" variables, i'll show you how it can be done (but usually there are reasons for doing this... which probably don't apply here). Note: in bash, a period '.' (as a command) is the same as the "source" builtin...

# SHELL SCRIPT:
$ cat foo
Code:


#!/bin/bash -
IFS=$' \t\n'
declare -x PATH=/bin:/usr/bin

. ~/.myConfigs

echo "\$0 = $0"
echo "\$1 = $1"
echo "\$2 = $2"
echo "\$3 = $3"
echo
echo "computer name    = $computerName"
echo "ethernet address = $etherAddress"
echo "boot disk name  = $bootDiskName"
echo
sleep 2

# assigning items to the positional parameters:
set -- "$computerName" "$etherAddress" "$bootDiskName"

# ^ REPLACES whatever positional parameters previously existed.
# Is that what you want?

echo "\$0 = $0"
echo "\$1 = $1"
echo "\$2 = $2"
echo "\$3 = $3"

exit $?

Note how (like hayne's post #2 example) that foo prints out 3 variables without ever declaring or initializing them. Here's the hidden config file it sources to get the data...

# CONFIG FILE:
$ cat .myConfigs
Code:


computerName=`/bin/hostname -s`
etherAddress=`/sbin/ifconfig en0 ether |/usr/bin/sed '2!d;s/^[^0]*//;s/[: ]//g'`
bootDiskName=$(/usr/sbin/diskutil info `/usr/sbin/bless --getBoot` |
/usr/bin/sed '/^ *Volume Name: */!d;s###')

. . .

And, here is what it looks like when it's run:
Code:


$ foo
$0 = /Users/halito/bin/foo
$1 =
$2 =
$3 =

computer name    = PowerBookG4
ethernet address = 000d93abcde0
boot disk name  = Hals_Mac

$0 = /Users/halito/bin/foo
$1 = PowerBookG4
$2 = 000d93abcde0
$3 = Hals_Mac

Try it out, and then decide. [i don't think reassigning the number variables offers any advantage here.]

BTW...
Quote:

>> So for example, I am looking at using functions
Functions also get assigned their own positional parameters (arg list), so that adds even more to the potential confusion. The body of a script gets its numbered variables and functions get whatever args we call them with.

tlarkin 03-19-2010 02:04 PM

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

Hal Itosis 03-19-2010 02:27 PM

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
$0 = /Users/halito/bin/foo
$1 = these
$2 = are
$3 = arguments

computer name    = PowerBookG4
ethernet address = 000d93abcde0
boot disk name  = Hals_Mac

$0 = /Users/halito/bin/foo
$1 = PowerBookG4
$2 = 000d93abcde0
$3 = Hals_Mac



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.