0

I want to pass a variable which I set in a subscript to the parent script. I want to do that either as a function or just by "echo"ing the variable:

parent.sh

#!/bin/csh
enter code here
set VAR_2_CHILD_1 = myName
set VAR_2_CHILD_2 = MAIN_PATH/output.txt

$MAIN_PATH/child.sh $VAR_2_CHILD_1 $VAR_2_CHILD_2

child.sh

#!/bin/csh

if ($1 =~ *something*) then
    set NAME_SL     =  False
    set SUBDIRID    =  True
    echo $1 $SUBIDDIR       >> $2
endif

echo $NAME_SL $SUBDIRID

or:

#!/bin/csh

function pass_var() {

if ($1 =~ *something*) then
    set NAME_SL     =  False
    set SUBDIRID    =  True
    echo $1 $SUBIDDIR       >> $2
endif

return $NAME_SL $SUBDIRID
}

In the later case I do not know how to call the function in the parent.sh

In general the subscript should choose between option according to $VAR_2_CHILD_1 and VAR_2_CHILD_2 and returning some string variable $NAME_SL and $SUBDIRID. I do not how to do that.

I also read other questions here like: Bash - Return value from subscript to parent script but I could not find anything which works for me.

1 Answer 1

0

Ahh! You gladden my heart. Wrote csh for a 10 year period (using csh, sed, awk, and pipes). Just checked my archive: 650 scripts and 51000 lines. Then, I discovered perl and switched everything. Current perl is 770 files and 206,000 lines.

Anyway, a few things. csh doesn't have functions, so you'll need to use child.sh. (Please use .csh or no suffix--bash gets far too much attention as it is :-)

Note that csh does have aliases. csh aliases are more powerful than bash aliases because they can take argument lists. You can get the effect of a multiline script using ; but it's limiting.

The parent can capture child output [either script or alias] via the virgule operator:

#!/bin/csh
# enter code here
set VAR_2_CHILD_1 = myName
set VAR_2_CHILD_2 = MAIN_PATH/output.txt

set result=(`$MAIN_PATH/child.sh $VAR_2_CHILD_1 $VAR_2_CHILD_2`)
set NAME_SL="$result[1]"
set SUBDIRNAME="$result[2]"

The parentheses around the child call are because the child is returning two words, so you need an array to capture them [if you want to manipulate them independently]

3
  • Thank you very much for your kind reply and help! It is working perfectly :-) ! Commented Nov 3, 2015 at 12:00
  • @firefly2517 Thanks for your politeness. My last 24 hours: hecklers in comments. They're wrong--my mother would never do that :-). And one, where an OP had a segfaulting program. We were debugging it in realtime with comments: try this, what result?, try this ... Worked with OP for 2 hours. Heckler hammered OP and OP withdrew the [+3] question [with 25 helpful comments]: I was about 15 min away from the solution. BTW, how did you decide to use csh? Was popular long time ago, now "It's a bash, bash, bash, bash world" (bash==mad) Even with perl, csh is still my login shell. Commented Nov 5, 2015 at 4:07
  • Life brought me to csh ;-), I just slipped into it but I like it. Somebody was showing me how to use .sh-files to configure my python programme. He is using csh and C, I am using csh and Python 2.7.10 now. Maybe I should switch to something more fancy, but I believe in simplicity! And it is working fine (at least sometimes ;-) )! Commented Nov 5, 2015 at 18:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.