Help in tcsh script

Hi All,
I wrote a tcsh script, but being a beginner it took me lots of efforts and on top of that I am still struggling with little modifications here and there.
kindly have a loop.

Line1 : I want it to run maximum of "Max" Which I am providing outside loop. So how the "for" should be modified,

foreach i in (0 1 2 3 .. $Max)  ???? 

Lin2 : I want this pause to be set as default

Line3: The "file$" is increasing each time, in the end of the loop I want to set this as default 1, i.e "file1"

Thanks in advance
Pooja

#!/bin/tcsh 
set TARGETPATH=$2
set Max=3
set Card='card_Data2011AB_Zee_40GeV'

#Function Loop                                                                                                                                                         
LINE 1 foreach i (0 1 2 3 4 5 6 7)
    echo "SHELL SCRIPTING ================ $i =============="
    cp $FILE $TARGETPATH

    if ( $i == 0 ) then
    source $TARGETPATH
    echo 'Press [Enter] key to continue...'
  LINE2  set thisvar = $<
    rm $TARGETPATH

    else if ( $i < $Max ) then
        perl -i.bak -pe 's/(file)(\d{1,})/$1 . (1 + $2)/ge' $Card
        cp $FILE $TARGETPATH
        source $TARGETPATH
        echo 'Press [Enter] key to continue...'
    set thisvar = $<
        rm $TARGETPATH

        else if ( $i == $Max ) then
LINE3           perl -pi.bak -e 's/file/file1/g' $Card                                                                                                                     
            echo "Its About To Terminate...Enjoy Scripting"
    endif
end

I'd recommend to abandon tcsh (or any other *csh) and learn shell that is worth its salt (e.g. bash, ksh, zsh, ...)

Hi,
thanks for the reply and for the link.
Even, I am more happier and comfortable with bash script.
Unfortunately, I have to run script on some remote machine where I am afraid only have tcsh SHELL
My little knowledge says it its UNIX machine, so both SHELL should be there. I am not sure if I can change the SHELL of some remote machine?

Thanks
Pooja

Just change the first line to a different shebang:

#!/bin/bash

or

#!/bin/sh

for bourne shell.

Unfortunately, I did not help.
I wrote the test script in my laptop with bash SHELL. and when I shifted it to the remote machine. It failed to work.
Actually, those machine are not usual one and the default environment settings pick the tcsh shell.
So I started from scratch and tryign to make it work it for tcsh.

Thanks,
Pooja

What exactly do you mean "It failed to work"? Was there an error message? Post your bash version.
Check whether bash is really where I assumed it to be:

ls /bin/bash

If you let us know what system are you on, your chances for getting accurate help will be much higher than us having to guess.

uname -a

You don't have to care what the login shell is. The shebang line is what is used to run the script, and your machine does have sh. It must have one somewhere, too many things in UNIX demand it internally.

If you don't know where it is, which sh or whereis sh

Linux cmslpc10.fnal.gov 2.6.18-274.12.1.el5 #1 SMP Tue Nov 29 11:14:39 EST 2011 x86_64 x86_64 x86_64 GNU/Linux

---------- Post updated 2012-08-18 at 07:28 AM ---------- Previous update was 2012-08-17 at 12:32 PM ----------

Hi,
I am not sure as how shall I go ahead with it. I mean changing the shell or something.

Here is the output of the following command:

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

I would be really happy if bash scripts can run there. Just to mention, I do use simple bash scripts there. And they work. But this particular one failed.

Thanks
Pooja

OK, let me spell it for you. Literal transcription into bash:

#!/bin/bash  

TARGETPATH=$2 
Max=3 
Card=card_Data2011AB_Zee_40Ge

#Loop  (no function here)
for i in {0..7} ; do     
    echo "SHELL SCRIPTING ================ $i =============="
    cp $FILE $TARGETPATH
    if [[ $i -eq 0 ]] ; then
         source $TARGETPATH
         echo 'Press [Enter] key to continue...'
         read thisvar
         rm $TARGETPATH
     elif [[ $i -lt $Max ]] ; then
         perl -i.bak -pe 's/(file)(\d{1,})/$1 . (1 + $2)/ge' $Card
         cp $FILE $TARGETPATH
         source $TARGETPATH
         echo 'Press [Enter] key to continue...'
         read thisvar
         rm $TARGETPATH
       elif [[ $i -eq $Max ]] ; then
            perl -pi.bak -e 's/file/file1/g' $Card
            echo "Its About To Terminate...Enjoy Scripting"
      fi
done

Fixed that for you.

Not all Bourne shells are BASH. BASH is a Bourne shell, with extended features that a generic Bourne shell doesn't have -- like arrays, and functions.

There is also more than one version of BASH. Some of them support more things than others. Only very new versions of BASH have direct regex support for example, i.e. [[ "$STRING" =~ /regex/ ]] so don't use that if you don't know your system will have have BASH and a very new bash at that.

Avoiding BASH features will make your scripts much closer to generic Bourne scripts, which should theoretically work equally well in bash, sh, and ksh. You may be surprised how much you can do without them; a lot of things people use arrays for can be replaced with set -- , proper use of the read command and IFS, or simple string-splitting, for instance.

2 Likes

Thanks Corona for the useful information.
It is resolved now.

Pooja