Error in script, STUCK :(

Hi All,
I am beginner in scripting. I wrote a simple script to perform some task. It seem to have some error in command line,

Kindly somebody help.
Thanks

#!/bin/bash
date
cd /home/poojasaxena/Desktop/CMS/script/DataMCMatch

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

FILE=$1
TARGETPATH=$2

max=2
for ((i=1; i<=$max; ++i )) ; 
do
    echo "$i  ..." 
    cp $FILE $TARGETPATH
    #cp DataMC.sh  temp.sh
    
    if[ $i == 1] ; 
    then
	echo "for first set of files"
	sed  -e 's/file1.list/file2.list/g' card_Data2011AB_Zee_40GeV > card_Data2011AB_Zee_40GeV_1
	sed  -e 's/file1.root/file2.root/g' card_Data2011AB_Zee_40GeV_1 > card_Data2011AB_Zee_40GeV_2
	mv card_Data2011AB_Zee_40GeV_2 card_Data2011AB_Zee_40GeV
	
	echo "its done"
	source $TARGETPATH
	
	pause 'Press [Enter] key to continue...'
	rm $TARGETPATH
    else
	echo "Its about to terminate"
    fi
done                                                                                                                                  
                                                                                                                                                                       

The Error is the following

poojasaxena@poojasaxena-laptop:~/Desktop/CMS/script/DataMCMatch$ ./Script4CondorJobs.sh  DataMC.sh temp.sh
Thu Aug 16 23:12:44 IST 2012
./Script4CondorJobs.sh: line 20: syntax error near unexpected token `then'
./Script4CondorJobs.sh: line 20: `    then'

This is wrong, you're missing a space:

    if[ $i == 1] ; 
    if[ $i == 1 ]

The semicolon on the end was also pointless, but harmless too. ; separates multiple commands on a line, you don't need to use it to end a line.

1 Like

pls leave spaces around [ and ].
@Corona668: the [ needs a leading space as well.

1 Like

The square brackets must be surrounded by space characters.
Also, lose the semi-colons - this is Shell not Oracle.

   if [ $i == 1 ]

Hi all,
I am so thankful for you guys for such prompt replies.
well, as I understood I changed the section of the code as


    if[ $i == 1 ]
    then
        echo "for first set of files"

But still it did not help :frowning: And I am facing the same error as I mentioned in the previous original message.

There must be some other error. Kindly have a look

Thanks
Pooja

pls leave spaces around BOTH [ and ].

Hi,
Isnt it correct now??

if[ $i == 1 ]
    then

I am still facing the same original error. :frowning:

Thanks
Pooja

Leave a space character after if (or any unix command for that matter).

1 Like
if [ $i == 1 ]
  ^ THIS space!
1 Like

Great,
Lots of thanks you guys for such prompt help.. :slight_smile:

    if [ $i == 1 ]
    then

Working fine for now. Trying to modify the script further..:slight_smile: fingers crossed.

Thanks again,
Pooja

---------- Post updated at 01:28 PM ---------- Previous update was at 01:06 PM ----------

Hi,
I have small query,
ummm, using sed for changing the common text in a file leads to generation of extra file. Which becomes unnecessary step sometimes.

As I used sed is:

sed -e 's/file1/file2/g' Data1.txt > Data2.txt

For me purpose, I have to mv Data2.txt to Data1.txt anyhow. Please let me know if there is some trick to take care of it.

Thanks
Pooja

unless your sed supports the '-i', you can try this:

{ rm FILE; sed -e '...' > FILE; } < FILE

Hi,
The final form of the script is the following [1].. It's fulfilling my purpose which is following,
I need to run a card name

card_Data2011AB_Zee_40GeV 

using a shell script Which I am providing as an argument.
And every time, I need to change the variable

 filei to filei+1 .
I am afraid code is working, but I need to repeat this if else 

some 15 times or maybe more.
Can somebody please shrink the content?

Thanks in advance,
Pooja

[1]

#!/bin/bash
date
cd /home/poojasaxena/Desktop/CMS/script/DataMCMatch

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

FILE=$1
TARGETPATH=$2

max=3
for ((i=1; i<=$max; ++i )) ; 
do
    echo "SHELL SCRIPTING ================ $i ==============" 
    cp $FILE $TARGETPATH
    
    if [ $i == 1 ]
    then  
	source $TARGETPATH
	
	pause 'Press [Enter] key to continue...'
        rm $TARGETPATH
	
    else
	if [ $i == 2 ]
	then
	    sed  -e 's/file1/file2/g' card_Data2011AB_Zee_40GeV > card_Data2011AB_Zee_40GeV_$i
	    cp card_Data2011AB_Zee_40GeV_$i card_Data2011AB_Zee_40GeV
	    source $TARGETPATH
	    
	    pause 'Press [Enter] key to continue...'
	    rm $TARGETPATH
	else
	    if [ $i == 3 ]
            then
		sed  -e 's/file2/file1/g' card_Data2011AB_Zee_40GeV > card_Data2011AB_Zee_40GeV_$i
		cp card_Data2011AB_Zee_40GeV_$i card_Data2011AB_Zee_40GeV
		source $TARGETPATH
		
		pause 'Press [Enter] key to continue...'
		rm $TARGETPATH
	    else
		echo "Its About To Terminate...Enjoy Scripting"
	    fi
	fi
    fi
done

---------- Post updated at 01:44 PM ---------- Previous update was at 01:38 PM ----------

Trick worked :slight_smile:

thanks,

---------- Post updated at 02:27 PM ---------- Previous update was at 01:44 PM ----------

Hi,
I managed to shrink it, here is the script [1]. It is working fine EXCEPT that sed command has the error :confused:

 { rm card_Data2011AB_Zee_40GeV; sed -e 's/file$i/file$(i+1)/g' > card_Data2011AB_Zee_40GeV; } < card_Data2011AB_Zee_40GeV 

I want this " i " to increment each time so its like :

i = 1 file1 -> file2
i = 2 file2-> file3
and so one 

Please have a look,
Thanks

[1]

#!/bin/bash
date
cd /home/poojasaxena/Desktop/CMS/script/DataMCMatch/

FILE=$1
TARGETPATH=$2

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

max=3
for (( i=0; i<=$max; ++i )) ; 
do
    echo "SHELL SCRIPTING ================ $i ==============" 
    cp $FILE $TARGETPATH
    
    if [ $i == 0 ]; then
	source $TARGETPATH
	pause 'Press [Enter] key to continue...'
        rm $TARGETPATH
	
    else
	if [ $i > 0 ]; then
	    { rm card_Data2011AB_Zee_40GeV; sed -e 's/file$i/file$i++/g' > card_Data2011AB_Zee_40GeV; } < card_Data2011AB_Zee_40GeV
	    source $TARGETPATH
	    pause 'Press [Enter] key to continue...'
	    rm $TARGETPATH
	else
	    echo "Its About To Terminate...Enjoy Scripting"
	fi
    fi
done

you can use a C like syntax like for integers

if (( $i == 1 ))
then
.............
fi

Hi Praveen,
Thanks for the reply, but I did not get this,
I want it to be implemented in SED command.. :confused:
Can you please write the command?

Thanks
Pooja

First: replace single quotes with double quotes: this way the "$" will mantain its special value.
Second: use the arithmetic expansion syntax:

sed -e "s/file$i/file$((i+1))/g"

or:

sed -e "s/file$i/file$(($i+1))/g"

which are the same thing.
--
Bye

Perfect, Got it..:):slight_smile: Working all fine..

Thanks,
Pooja

---------- Post updated at 04:28 PM ---------- Previous update was at 02:52 PM ----------

Hi all,
I am completely messed up. :wall::wall:
I wrote this script locally where I used the bash SHELL. But originally I need to use it at some other machine where the SHELL is tcsh. :(:frowning:

tcsh is totally new for me. if somebody here know tcsh scripting and can help me. It would save lot of my time.

Thanks in advance
pooja

working script in bashrc


#!/bin/bash
date
cd /home/poojasaxena/Desktop/CMS/script/DataMCMatch/

FILE=$1
TARGETPATH=$2

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

max=3
for (( i=0; i<=$max; ++i )) ; 
do
    echo "SHELL SCRIPTING ================ $i ==============" 
    cp $FILE $TARGETPATH
    
    if [ $i == 0 ]; then
	source $TARGETPATH
	pause 'Press [Enter] key to continue...'
        rm $TARGETPATH
	
    else
	if [ $i > 0 ]; then
	    { rm card_Data2011AB_Zee_40GeV; sed -e "s/file$i/file$((i+1))/g" > card_Data2011AB_Zee_40GeV; } < card_Data2011AB_Zee_40GeV
	    source $TARGETPATH
	    pause 'Press [Enter] key to continue...'
	    rm $TARGETPATH
	else
	    echo "Its About To Terminate...Enjoy Scripting"
	fi
    fi
done

tcsh isn't even a bourne shell.

If the machine is UNIX at all, it will have a /bin/sh or at least some sort of sh, you don't have to use tcsh.

Otherwise, you're starting over from scratch.

Hi,
yeah, Machine is UNIX. I being a physics student not much aware of this stuff.
For example, I do not know if I can run this script at all in tcsh SHELL based machine.
I already started form scratch [1], script showing error in "if" command. My unlucky day :frowning:

[1]

#!/bin/tcsh
date

# cd /uscms_data/d3/pooja04/Area4VGamma/CMSSW_4_2_8_patch7/src/ElectroWeakAnalysis/MultiBosons/
# source /uscmst1/prod/sw/cms/setup/cshrc prod
# cmsenv

# Code Directory
cd /uscms/home/pooja04/script/TagProbe/KuanCode/test/WithJson/2012/version4/data/v5/

################################################################################################################

# FILE = file.sh whhich execute 'root -l RunAnaVgNtuple.C'
# TARGETPATH = temp copy of file.sh
echo "0"
set FILE=$1
set TARGETPATH=$2
echo "1"

#function pause(){
#read -p "$*"
#}
echo "2"
set max=12

    foreach i (1 2 3 4 5 6 7 8 )
    #    do
    echo "SHELL SCRIPTING ================ $i ==============" 
    cp $FILE $TARGETPATH
    echo "is it "
    if ( i < 1 ) then
        echo "4"
        source $TARGETPATH
        echo "5"
#        pause 'Press [Enter] key to continue...'
        echo "6"
        rm $TARGETPATH

    else if ( $i > 0 ) then
        { rm card_Data2011AB_Zee_40GeV; sed -e "s/file$i/file$((i+1))/g" > card_Data2011AB_Zee_40GeV; } < card_Data2011AB_Zee_40GeV
        source $TARGETPATH
        pause 'Press [Enter] key to continue...'
        rm $TARGETPATH
    else
        echo "Its About To Terminate...Enjoy Scripting"
    endif
    end
    

If the machine is UNIX at all, it will have a /bin/sh or at least some sort of sh, you don't have to use tcsh.

Try which sh or whereis sh

Hi,
Here are the output of the folllowing commands:

[pooja04@cmslpc06 ~]$ which sh
/bin/sh
[pooja04@cmslpc06 ~]$ whereis sh
sh: /bin/sh /usr/share/man/man1/sh.1.gz /usr/share/man/man1p/sh.1p.gz
[pooja04@cmslpc06 ~]$ 

So even if there is sh shell, how it should be switched from tcsh to bash?

thanks