Author Topic: sh script : Why do this ?  (Read 2212 times)

gab74

  • Sr. Member
  • ****
  • Posts: 344
    • View Profile
sh script : Why do this ?
« on: July 07, 2004, 07:54:37 am »
I make a simply sh script

test.sh

export PATH=/mnt/card/gcc/bin:$PATH

When i run the script from console as root, the script is executed but PATH still remains unchanged.

if i type the command from console ; it works !!!

Why this ????

I use my SL6000L

Why cannot change PATH with a script....on lInux it works !!!
Gabriele
NOW : C3100 (code name Laudicus)- SOCKETCOMM CF MODEM 56K - CF GPS GLOBASAT BC-307 - BLUETOOTH CF BELKIN - ETHERNET CF TRENDNET TE-CF100
BEFORE: SL6000L (code name Anselmus) - 512MB SD - 256MB CF - SOCKETCOMM CF MODEM 56K - CF GPS GLOBASAT BC-307

tumnus

  • Hero Member
  • *****
  • Posts: 1176
    • View Profile
    • http://www.cpinkney.org.uk
sh script : Why do this ?
« Reply #1 on: July 07, 2004, 08:57:29 am »
You need to type:

source test.sh

Running a script will simply set the path within the script itself. 'sourcing' a script means all the environment variable changes in the script are applied to the current shell.
# Search the Zaurus Howtos ## Search the Zaurus FAQs ## Find Z software at ELSI #
--------------------
UK SL5500 with Sharp ROM 3.13, SL5600 with Sharp ROM 1.32 - SuSE 9.0 Pro, Windows XP Home
Qualendar for Calendar and Todo
Socket Bluetooth CF Card (Rev F), Kingmax 512MB MMC Card, Palm Tungsten T Stylus,
Pretec CF->Smartmedia Adapter, Semsons Universal Battery Extender

gab74

  • Sr. Member
  • ****
  • Posts: 344
    • View Profile
sh script : Why do this ?
« Reply #2 on: July 07, 2004, 09:44:03 am »
OK thank you very much for your help ! it works !
Gabriele
NOW : C3100 (code name Laudicus)- SOCKETCOMM CF MODEM 56K - CF GPS GLOBASAT BC-307 - BLUETOOTH CF BELKIN - ETHERNET CF TRENDNET TE-CF100
BEFORE: SL6000L (code name Anselmus) - 512MB SD - 256MB CF - SOCKETCOMM CF MODEM 56K - CF GPS GLOBASAT BC-307

ScottYelich

  • Hero Member
  • *****
  • Posts: 992
    • View Profile
    • http://www.zaurususergroup.com/modules.php?opmodload&namephpWiki&filei
sh script : Why do this ?
« Reply #3 on: July 07, 2004, 01:52:36 pm »
Quote
OK thank you very much for your help ! it works !
actually, you need to understand what is going on....

(and people can correct me, etc).

When you make:
test.sh containt
PATH=${PATH}:/new/path/component
export PATH


and then you run this... your new PATH *is* being set.... in that script... in the "environment" for that
script -- and for the environment in the children of that script... but, when the script ends... that
environment ends.

If you want that command to affect your *current* environment -- you can consider your current
environment the "parent" and that script the "child" ... and you'll probably come across it said that
children can't modify the environment of their parent -- and although this isn't entirely true, it's
mostly good enough for now.

So, in your current environment, you need to update your path.... so, instead of creating a new environment
by running a shell and executing that script, since you're already IN a shell, just load that script into your
current environment --

. /path/to/test.sh

etc.

source is [t]csh .. and I don't know if bash/ksh have it -- the ksh that I am use to does not.
sh has traditionally used "."

You may also see programs that will do things like this (ie: check the start up of any  unix/linux box)

#!/bin/sh

. /path/to/ENV

command1
command2
command3


The idea there is that someone puts commands in the "ENV" that make things work -- but then that
environment is needed in many different locations -- and instead of copying it each time (which would work) or calling the script like you did before "/full/path/to/test.sh" (child) which would NOT affect the
current (or parent) environment, the "." is used.

The different is the "relative" environment.  Current (parent) or new (child) -- where the child can not
change the parent's env (for the most part, this should be remembered).


most all unix problems/issues come from "path" or "permissions" ... although the example above probably
isn't either, I thought it was woth mentioning (also, it's just my opinion).

Scott