Issue in TC Shell Script

I have a script in TC shell, for some reason its not working.

I'm trying to kick off a script if there are any files in a particular directory "/sample/test3"

Can anyone point out the issue in the same

#!/usr/bin/tcsh
while true
do
    if ls /sample/test3 >& /dev/null  ; then /sample/app_code/run/initiateLoad.sh ; endif
    sleep 1800
done

Open in new window


The path /sample/test3 contains files so the if condition should have been successful and should have started the shell script, but its not happening.
gaugetaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TintinCommented:
That's not valid tcsh syntax.   I'd use sh/ksh/bash, eg:

#!/bin/sh
while true
do
    [ -n "`ls /sample/test3`" ] && /sample/app_code/run/initiateLoad.sh
    sleep 1800
done

Open in new window

0
gaugetaAuthor Commented:
@Tintin : Thanks for the reply.

Its a requirement that I use tcsh.

Can you provide the alternate code for tcsh for the same.
0
TintinCommented:
tcsh is an awful scripting language, but if you have no choice, then do

#!/bin/tcsh
while (1)
  ls /sample/test3/* >& /dev/null

  if ( $? == 0 ) then
     /sample/app_code/run/initiateLoad.sh
  endif

  sleep 1800
end

Open in new window

0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

gaugetaAuthor Commented:
@Tintin : Thanks for the reply.

I tried your script and regardless of whether a file is present in the location "/sample/test3/" the "$?" variable is returning 0.

What is the "$?" variable represent.

How can I fix this issue.
0
TintinCommented:
What version of tcsh are you running?

Do

tcsh --version

Works fine on my system with tcsh 6.17.00

$? contains the return status from the previous command, so if the ls fails, ie: doesn't match any files, it will have an exit status of 1.  If it matches files, it will have an exit status of 0.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gaugetaAuthor Commented:
@Tintin : I'm running tcsh 6.11.00

Can you give any suggestions fir this version.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.