passing argument from one function to another

Hi all,
In the given script code [1].
I want to pass the maximum value that variable "i" will have in function DivideJobs () to variable $max of function SubmitCondorJob().

Any help?
Thanks

#!/bin/bash                                                                                                                                                            
date
PATHNAME=/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/
FileName=DataFileName
#TARGETPATH=                                                                                                                                                           
SCRIPT=DataMc.sh
TEMPSCRIPT=Temp.sh
CARD=card_Data2011AB_Zee_40GeV
CondorJob=Job_condor

CopyFilesInfo() {
#    ls -ltr "$PATHNAME" | grep root | awk '{print string path $9}' string="$CONSTANT" path="$1"  > "$FileName"                                                        
    ls -ltr $PATHNAME grep root | awk '{print "$PATHNAME"$9}' > "$FileName"
    echo "FileName are Copied"
}


DivideJobs() {
    echo "Division Of Jobs"
    #split -$4  $3 chunk                                                                                                                                               
    split -10  $FileName chunk

    i=0
    for file in chunk*
      do
      ((i=i+1))
      new_file="data"$i".list"
      perl -0pe 's/\n$//' $file >  $new_file

      #to remove the processed file                                                                                                                                    
      rm -rf $file
      echo $i ' succesful'
    done
}

#For Submiting the jobs                                                                                                                                           
SubmitCondorJob () {
    max=$2
    for (( i=0; i<=$max; ++i )) ;
      do
      echo "SHELL SCRIPTING ================ $i =============="
      new_file="temp"$i".sh"
      cp $SCRIPT $new_file

      if [ $i == 0 ]; then
          source $new_file
          pause 'Press [Enter] key to continue...'
          rm $new_file
      else
          if [ $i > 0 ]; then
              { rm $CARD; sed -e "s/file$i/file$((i+1))/g" > $CARD; } < $CARD
              source $new_file
              pause 'Press [Enter] key to continue...'
              rm $new_file
          else
              echo "SUCCESFULLY DONE.."
          fi
      fi
    done
}

if ["$1" = "condorjob" ]; then
    path
    CopyFilesInfo
    DivideJobs
    SubmitCondorJob
    exit 0
fi


Just change the 'max' assignment at the beginning of "SubmitCondorJob" to use $i since you do not have it declared as a local variable to function(DivideJobs):

max=$i

You can use spacebar's solution..

Also you simply define the variable in DivideJobs function

DivideJobs() { 

max=$i
}

Hi, Thanks for the replies.

I changed the code and made the following changes [1]. The Error I am facing is following:

 ./Script4JobSubmit.sh: line 96: ((: k <  : syntax error: operand expected (error token is " ") 

Even I change the k <= $max it does not help.

pooja

#!/bin/bash
date

# Global Parameters
#PATHNAME=/eos/uscms/store/user/pooja04//analysis2012/525/data/0001/data/30Aug2012/   #533
PATHNAME=/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/
FileName=DataFileName
SCRIPT=DataMC.sh
TEMPSCRIPT=temp.sh
CARD=card_Data2011AB_Zee_40GeV
CondorJob=Job_condor
max=$i

function pause(){
   read -p "$*"
}

usage () {
    echo "Script to submit offline batch mode jobs"
    echo "Possible arguments:"
    echo "  batchjob - to submit batch jobs"
    echo "  condorjob - to submit batch jobs"
    echo "  help - shows this help"
    echo "   ./Script4OfflineJobs.sh batchjob"
}

# this sets CLASSPATH                                                                                                                                                  
path () {
    source /uscms/home/pooja04/.bashrc
    source /uscms/home/pooja04/ToBegin.sh
    cd /uscms_data/d3/pooja04/CMSSW_5_3_3_patch2/src/ElectroWeakAnalysis/MultiBosons/test
    cmsenv
    cd /uscms/home/pooja04/script/AnalysisCode/Code42012/v2
}


#To Copy the Files Inforamtion from the Destination Folder
CopyFilesInfo() {
    ls -ltr "$PATHNAME" | grep root | awk '{print string path $9}' string="$CONSTANT" path="$PATHNAME"  > "$FileName"
#    ls -ltr $PATHNAME |  grep root | awk '{print "$PATHNAME"$9}' > "$FileName"
    echo "FileName are Copied"
}

#Divide the jobs in small files
DivideJobs() {
    echo "Division Of Jobs"
    #split -$4  $3 chunk
    split -10  $FileName chunk
    
    i=0
    for file in chunk*
      do
      ((i=i+1))
      new_file="data"$i".list"
      perl -0pe 's/\n$//' $file >  $new_file
      rm -rf $file
      echo $i ' succesful'
    done
}

#For Submiting the batchjobs
SubmitBatchJob () {
    for (( k=0; k<= "$max" ; ++k )) ; 
      do
      echo "SHELL SCRIPTING ================ $i ==============" 
      new_file="temp"$k".sh"
      cp $SCRIPT $new_file
      
      if [ $k == 0 ]; then
	  source $new_file
	  pause 'Press [Enter] key to continue...'
	  rm $new_file
      else
	  if [ $k > 0 ]; then
	      { rm $CARD; sed -e "s/data$i/data$((k+1))/g" > $CARD; } < $CARD
	      source $new_file
	      pause 'Press [Enter] key to continue...'
	      rm $new_file
	  else
	      echo "SUCCESFULLY DONE.."
	  fi
      fi
    done
}    


#For Submiting the batchjobs
SubmitCondorJob () {

    for (( k=0; k < $max ; k++ ))
      do
      echo "CondorJob Submission For  ================ $i ==============" 
      new_file="temp"$k".sh"

      if [ $k == 0 ]; then
#      cp $SCRIPT $TEMPSCRIPT
	  echo "$SCRIPT"
	  cp $SCRIPT "temp.sh"

	  condor_submit $CondorJob
#	  rm $TEMPSCRIPT
	  rm "temp.sh"
	  echo "Going To Sleep.."
	  sleep 100
      else
	  if [ $k > 0 ]; then
	      { rm $CARD; sed -e "s/data$i/data$((k+1))/g" > $CARD; } < $CARD
	 #     cp $SCRIPT $TEMPSCRIPT 
	      cp $SCRIPT "temp.sh"
	      condor_submit $CondorJob
#	      rm $TEMPSCRIPT
	      rm "temp.sh" 
	      echo "Going To Sleep.."
	      sleep 100
	      echo "SUCCESFULLY DONE.."
	  fi
      fi
    done
}    

if [ $# -lt 1 ]; then
    usage
    exit 1
fi

if [ $1 = "help" ]; then
    usage
    exit 0

elif [ "$1" = "batchjob" ]; then
    path  
    CopyFilesInfo
    DivideJobs
    SubmitBatchJob
    exit 0

elif [ "$1" = "condorjob" ]; then
#    path
    CopyFilesInfo 
    DivideJobs
    SubmitCondorJob
    exit 0
fi


In the beginning, the variable max has been assigned the value of i (which at that particular instant has not been set, I assume). Hence, the error in the for loop. Assign a proper numeric value to max . Also, that is an arithmetic for loop so no need to double-quote values. If you still want to go ahead with a null value, use typeset on max :

typeset -i max=$i

At the start defining max=$i doesn't make sense... at start where you have not defined i yet...

Have you tried with spacebar's or mine solution..?

I defiend this at the begginng of the script as :

 TEMPSCRIPT=temp.sh
CARD=card_Data2011AB_Zee_40GeV
CondorJob=Job_condor
typset -i max=$i
 

Hi, thank for the quick reply.
but it did not work. The error is following:

 [pooja04@cmslpc14 v2]$ ./Script4JobSubmit.sh condorjob
Sat Sep  1 02:08:47 CDT 2012
./Script4JobSubmit.sh: line 18: typset: command not found
FileName are Copied
Division Of Jobs
1  succesful
2  succesful
3  succesful

Hey, sorry for that. I'd made a typo in my post (rectified now). It ought to be typeset and not typset

1 Like

Hi,
I guess i interpreted it wrongly. I thought i need to make it global variable.
It worked, great..:slight_smile:

ummm, I would like to modify this script to do the following

 PATHNAME=/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/ 

I want the

 PATHNAME 

to be like some arrray to hole the information of several path so that I can simply get certain information from several of directories together.

Kindly suggest

Thanks again
Pooja

1) What you can do here is ...
Just create an array ( i don't know how is your directory structure, from your post it look like there is no pattern so we can get those path).
just give paths in a array.

path[1]="/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/"
path[2]="/eos/uscms/store/user/pooja04//analysis2012/525/data/"

same as how many you want it to add...

Then use array to perform operation on all paths....

for i in ${path[@])}
do
PATHNAME=${path[$i]}
...
Your Script...
...
done

2) Second option is you can create one file which stores all the paths you want to perform operations and store it somewhere..

And use that file to get the paths for your script..

$cat Path_file
/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele
/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/

while read line
do
PATHNAME="$line"
..
Your script..
...
done<Path_file
1 Like

Hi,
ummm, confusion.
I want $FileName to add the string collected from the different directories. In the given case it would create a separate $FileName every time it will read the PATHNAME[i]..????

Kindly suggest
pooja

PATHNAME[1]="/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/"
PATHNAME[2]="/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav4/"

CopyFilesInfo() {
    for j in ${PATHNAME[@]}
      do
      PATHNAME=${path[$i]}
      ls -ltr "$PATHNAME" | grep root | awk '{print string path $9}' string="$CONSTANT" path="$PATHNAME"  > "$FileName"
      echo "FileName are Copied"
    done
}

If you want to copy collected data from all directories to one file only then use >> while adding data...
If $FileName is same for every time then it will create only one file. just add >>

hi,
ERROR :confused::

 ./Script4JobSubmit_v1.sh: line 49: /eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/: syntax error: operand expected (error token is "/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/")
 

Code is following:


PATHNAME[1]="/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/"
PATHNAME[2]="/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav4/"

#To Copy the Files Inforamtion from the Destination Folder                                                                                                             
CopyFilesInfo() {
    for j in ${PATHNAME[@]}
      do
      PATH=${PATHNAME[$j]}
      ls -ltr "$PATH" | grep root | awk '{print string path $9}' string="$CONSTANT" path="$PATH"  >> "$FileName"
      echo "$PATH are Copied"
    done
}

See if you can get something out of the following:

a[0]="HI"
a[1]="THERE"

for i in "${a[@]}"
do
 echo $i
done

HI
THERE

for i in ${!a[@]}
do
 echo $i
done

0
1

Hi,

Could you please post your modified script here..

Hi,
Yeah, working all fine...and getting the same output as you showed. No issues at all..

:wall:...Never mind:

CopyFilesInfo() {
    for j in "${PATHNAME[@]}"
      do
      Path=$j
      ### or use $j directly
      .
      .
    done
}

There is someting wrong: :confused::confused::confused:
the script is here [2] and the error is [1]

[1]

  
Stage 1
Stage 2
Stage 4
stag5
stag6
./Script4JobSubmit_v1.sh: line 57: ls: command not found
./Script4JobSubmit_v1.sh: line 57: grep: command not found
./Script4JobSubmit_v1.sh: line 57: awk: command not found
/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/ are Copied
stag6
./Script4JobSubmit_v1.sh: line 57: ls: command not found
./Script4JobSubmit_v1.sh: line 57: grep: command not found
./Script4JobSubmit_v1.sh: line 57: awk: command not found
/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav4/ are Copied
Division Of Jobs
./Script4JobSubmit_v1.sh: line 66: split: command not found
./Script4JobSubmit_v1.sh: line 73: perl: command not found
./Script4JobSubmit_v1.sh: line 74: rm: command not found
1  succesful
CondorJob  ================ 0 ==============
./Script4JobSubmit_v1.sh: line 114: cp: command not found
./Script4JobSubmit_v1.sh: line 115: condor_submit: command not found
./Script4JobSubmit_v1.sh: line 117: rm: command not found
Going To Sleep..
./Script4JobSubmit_v1.sh: line 119: sleep: command not found
[pooja04@cmslpc01 v2]$ 

[2]

  
#!/bin/bash
date
################################################
# Script written by Pooja Saxena               #
# Fri, Aug 17-2012, 00:20 hr                   # 
# Usage ./script DataMC.sh temp.sh             #
################################################

# Global Parameters
#PATHNAME=/eos/uscms/store/user/pooja04//analysis2012/525/data/0001/data/30Aug2012/   #533
PATHNAME[1]="/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/"
PATHNAME[2]="/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav4/"

FileName=DataFileName
SCRIPT=DataMC.sh
TEMPSCRIPT=temp.sh
CARD=card_Data2011AB_Zee_40GeV
CondorJob=Job_condor
#typset -i max=$i

function pause(){
   read -p "$*"
}

usage () {
    echo "Script to submit offline batch mode jobs"
    echo "Possible arguments:"
    echo "  batchjob - to submit batch jobs"
    echo "  condorjob - to submit batch jobs"
    echo "  help - shows this help"
    echo "   ./Script4OfflineJobs.sh batchjob"
}

# this sets CLASSPATH                                                                                                                                                  
path () {

    echo "Stage 0"
    source /uscms/home/pooja04/.bashrc
    echo "Stage 1"
  #   source /uscms/home/pooja04/ToBegin.sh
    cd /uscms_data/d3/pooja04/CMSSW_5_3_3_patch2/src/ElectroWeakAnalysis/MultiBosons/test
    echo "Stage 2"
    eval `scram runtime -sh`
 #   cmsenv
    cd /uscms/home/pooja04/script/AnalysisCode/Code42012/v2
    echo "Stage 4"
}


#To Copy the Files Inforamtion from the Destination Folder
CopyFilesInfo() {
    echo "stag5"
    for j in "${PATHNAME[@]}"
      do
      echo "stag6"
      PATH=$j
      ls -ltr "$PATH" | grep root | awk '{print string path $9}' string="$CONSTANT" path="$PATH"  >> "$FileName"
      echo "$PATH are Copied"
    done
}

#Divide the jobs in small files
DivideJobs() {
    echo "Division Of Jobs"
    #split -$4  $3 chunk
    split -10  $FileName chunk
    
    i=0
    for file in chunk*
      do
      ((i=i+1))
      new_file="data"$i".list"
      perl -0pe 's/\n$//' $file >  $new_file
      rm -rf $file
      echo $i ' succesful'
    done
}

#For Submiting the batchjobs
SubmitBatchJob () {
    for (( k=0; k<= $max ; ++k )) ; 
      do
      echo "SHELL SCRIPTING ================ $i ==============" 
      new_file="temp"$k".sh"
      cp $SCRIPT $new_file
      
      if [ $k == 0 ]; then
	  source $new_file
	  pause 'Press [Enter] key to continue...'
	  rm $new_file
      else
	  if [ $k > 0 ]; then
	      { rm $CARD; sed -e "s/data$i/data$((k+1))/g" > $CARD; } < $CARD
	      source $new_file
	      pause 'Press [Enter] key to continue...'
	      rm $new_file
	  else
	      echo "SUCCESFULLY DONE.."
	  fi
      fi
    done
}    


#For Submiting the batchjobs
SubmitCondorJob () {
    max=$i
    for (( k=0; k < $max ; k++ ))
      do
      echo "CondorJob  ================ $k ==============" 
      TEMPSCRIPT="temp"$k".sh"

      if [ $k == 0 ]; then
	  cp $SCRIPT $TEMPSCRIPT
	  condor_submit $CondorJob
#	  { rm $CondorJob; sed -e "s/temp$k/temp$((k+1))/g" > $CondorJob; } < $CondorJob
	  rm $TEMPSCRIPT
	  echo "Going To Sleep.."
	  sleep 50
      else
	  if [ $k > 0 ]; then
	      { rm $CARD; sed -e "s/data$k/data$((k+1))/g" > $CARD; } < $CARD
	      cp $SCRIPT $TEMPSCRIPT 
	      { rm $CondorJob; sed -e "s/temp$k/temp$((k+1))/g" > $CondorJob; } < $CondorJob
	      condor_submit $CondorJob
	      rm $TEMPSCRIPT
	      echo "Going To Sleep.."
	      sleep 50
	      echo "SUCCESFULLY DONE.."
	  fi
      fi
    done
}    

if [ $# -lt 1 ]; then
    usage
    exit 1
fi

if [ $1 = "help" ]; then
    usage
    exit 0

elif [ "$1" = "batchjob" ]; then
    path  
    CopyFilesInfo
    DivideJobs
    SubmitBatchJob
    exit 0

elif [ "$1" = "condorjob" ]; then
    path
    CopyFilesInfo 
    DivideJobs
    SubmitCondorJob
    exit 0
fi
    


#$EXTRA COMMAND
#find /eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/ -name '*.root' | xargs ls -1tr | tee listfile | while read LINE

Just an observation:

much of the confusion is caused by using "i" or "j" instead of meaningful variable names. If a variable is an index to a filename, then call it "FileNameIdx" or something such, instead of "i". Once you accumulate several "i"s, "j"s and "k"s you sure will not know any more which counts what.

Do yourself a favour and don't save on the few keystrokes. You will save in the long run with less debugging effort what you invest up front in clarity.

I hope this helps.

bakunin

1 Like

Use below thing ...

And one most important thing don't use PATH as variable because bash store it's basic command path to the PATH variable...( Once Restart Your session) and run it...

This time it will work for sure...:slight_smile:

CopyFilesInfo() {
 for j in "${PATHNAME[@]}"
      do
      echo "stag6"
      
      ls -ltr "$j" | grep root | awk '{print string path $9}' string="CONSTANT" path="$j"  
      
      echo "$j are Copied"
    done
    
    }
1 Like