LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   pass variable from one shell script into another shell script (http://www.linuxquestions.org/questions/programming-9/pass-variable-from-one-shell-script-into-another-shell-script-759330/)

xskycamefalling 10-02-2009 10:33 PM

pass variable from one shell script into another shell script
 
so this might be a stupid question im not sure.. im still a bit new to all of this

i have some messy code that i wrote a while back and im trying to organize it..

the program opens xpaint and uses xwit and xte to draw packman and so on..

i want to split it all up im going to add more to it..

basically like i would want drawing packman in one function i guess you could call it and then coloring him or the backround in another


<code>
#!/bin/bash
pkill xpaint
res=`xwininfo -root | grep geom | sed -e 's/^.*try \([0-9][0-9]*x[0-9][0-9]*\)+.*$/\1/'`
Xoriginal=`echo $res | cut -d 'x' -f 1`
Yoriginal=`echo $res | cut -d 'x' -f 2`

X=`echo "($Xoriginal/2)-400" | bc `
Y=`echo "$Yoriginal/2" | bc `
#t=$(($Y/2))
bob=`echo "$Y/2" |bc `



echo $X
echo $Y

/usr/bin/xpaint -size "$Xoriginal"x"$Yoriginal" -popped &
sleep 4
paintersMom=`xwit -all -print | grep XP | cut -d ":" -f 1`
canvasWin=`xwit -all -print | grep Untitled | cut -d ":" -f 1`
#toolres=`xwininfo -id $paintersmom | grep geom | sed -e 's/^.*try \([0-9][0-9]*x[0-9][0-9]*\)+.*$/\1/'`
#canres=`xwininfo -id $canvasWin | grep geom | sed -e 's/^.*try \([0-9][0-9]*x[0-9][0-9]*\)+.*$/\1/'`

#echo $paintersMom
xwit -id $paintersMom -move 0 0
xwit -id $canvasWin -move 0 0
xwit -root -warp "$X" "$Y"
sleep 4

for((i=45; i < 315; i++))
do
xte 'mousedown 1'
ang=`./deg2rad $i`

movex=`echo "$X+$bob*c($ang)" | bc -l`
movey=`echo "$Y+$bob*s($ang)" | bc -l`
xwit -root -warp "$movex" "$movey"
done
xwit -root -warp "$X" "$Y"

xte 'mouseup 1'




#xwit -id $paintersMom -focus -raise
#sleep 1
#xwit -id $paintersMom -warp 30 390 #// fill in command on tool bar
#sleep 1
#xte 'mouseclick 1'
#sleep 1
#xwit -id $canvasWin -warp 221 40 #// color selection
#sleep 1
#xte 'mouseclick 1'
#sleep 1
#xwit -id $canvasWin -warp "$(($X-150))" "$Y" #//location on pac man
#sleep 1
#xte 'mouseclick 1'
#sleep 1





#xwit -id $canvasWin -warp 62 44 #// selecting color black
#sleep 1
#xte 'mouseclick 1'
#sleep 1
#xwit -root -warp 62 120 #// selecting back to canvas
#sleep 1
#xte 'mouseclick 1' #// color canvas black
#sleep 1
#xwit -id $paintersMom -focus -raise
#xwit -id $paintersMom -warp 27 108 #//click back to pencil
#sleep 1
#xte 'mouseclick 1'
#sleep 1




</code>


ok so here for example $paintersmom is would be in the drawing of pacman but i would also want to use it when i colored him in.. but i would want both processes in a diffrent shell script


thanks in advance sorry i cant explain this very well..

smeezekitty 10-02-2009 10:35 PM

command line parameters

xskycamefalling 10-02-2009 10:35 PM

sorry what do you mean? could you elaborate?

lutusp 10-02-2009 11:04 PM

Quote:

Originally Posted by xskycamefalling (Post 3705747)
sorry what do you mean? could you elaborate?

One shell script has number of ways to communicate with another.

1. Pass argument on the command line:

Code:

shell_script_name arg arg arg
(The second shell script receives these arguments as $1, $2, etc..)

2. Invoke one shell script within another like this:

Code:

. script-name (see the dot at the left?)
Invoking a script like this causes the declared variables from the invoked script to be inherited by the parent script.

3. Piping:

Code:

script_1 | script_2 | script_3
script_1 prints some information, which is piped to script_2, etc..

This doesn't exhaust the possibilities, but it should give you some ideas.

smeezekitty 10-02-2009 11:15 PM

pass the vaiables as parameters to the other shell script
like ./script2.sh var1 var2 var3

xskycamefalling 10-02-2009 11:41 PM

#!/bin/bash


pkill xpaint

# get monitor size

res=`xwininfo -root | grep geom | sed -e 's/^.*try \([0-9][0-9]*x[0-9][0-9]*\)+.*$/\1/'`
#
#get x and y value from monitor size

Xoriginal=`echo $res | cut -d 'x' -f 1`
Yoriginal=`echo $res | cut -d 'x' -f 2`
#
#finds center of screen -400 to move pacman to left
X=`echo "($Xoriginal/2)-400" | bc `
Y=`echo "$Yoriginal/2" | bc `
#
#
bob=`echo "$Y/2" |bc `

#
#call xpaint loader
./loadxpaint
#
#call pacman drawer
./drwpacman $Yoriginal $Xoriginal $X $Y $bob



is this what your saying??

./drwpacman $Yoriginal $Xoriginal $X $Y $bob

smeezekitty 10-03-2009 12:03 AM

yes
you have to change the child shell script
to read the command line and store it in new variables

xskycamefalling 10-03-2009 12:14 AM

so how would i use them.. im still getting errors

here is the code

MAIN
Code:

#!/bin/bash


pkill xpaint

# get monitor size

  res=`xwininfo -root | grep geom | sed -e 's/^.*try \([0-9][0-9]*x[0-9][0-9]*\)+.*$/\1/'`
#
#get x and y value from monitor size

  Xoriginal=`echo $res | cut -d 'x' -f 1`
  Yoriginal=`echo $res | cut -d 'x' -f 2`
#
#finds center of screen -400 to move pacman to left
  X=`echo "($Xoriginal/2)-400" | bc `
  Y=`echo "$Yoriginal/2" | bc `
#
#
  bob=`echo "$Y/2" |bc `

#
#call xpaint loader
  ./loadxpaint
#
#call pacman drawer
  ./drwpacman $Yoriginal $Xoriginal $X $Y $bob


~


here is DRWPACMAN


Code:

#!/bin/bash
#
#for loop for circle
. main
xte 'mousedown 1'
for((i=45; i < 315; i++))
do
ang=`./deg2rad $i`

movex=`echo "$X+$bob*c($ang)" | bc -l`
movey=`echo "$Y+$bob*s($ang)" | bc -l`
xwit -root -warp "$movex" "$movey"
done
xwit -root -warp "$X" "$Y"
xte 'mouseup 1'


smeezekitty 10-03-2009 12:23 AM

replace the variable names with $? where ? is a number from 1-9
some other member will help i really dont have time

ta0kira 10-03-2009 01:45 AM

The target-independent way to do this is to export the variables when assigning them; that way bash adds them to the process' environment rather than merely to the one the shell maintains itself, making it available within the fork's environment when the second script is executed.
Kevin Barry


All times are GMT -5. The time now is 07:33 AM.