Unix shell script fix issue

Hi ,

In my application there is  a piece of code .
The machine is aix and shell is ksh
file=$(ls $filepath | head -1);
echo File:$file

Open in new window

If i run them manually it showing a value for $file as /home/ee/20151030.txt

When i run them in script, it showing an empty value .
I used set -xv , then it shows as below , but not sure why the value is not assigned to variable $file, please advise.

file=$(ls $filepath | head -1);
[11]+[11]+ head -1
[11]+ ls /home/ee/20151030.txt
echo File:$file
[12]+echo File:
set +xv

Open in new window

I tried change this line as below, still same result.
file=$(ls $filepath | head -1);
file=`ls $filepath | head -1`;

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Gerwin Jansen, EE MVETopic Advisor Commented:
I wouldn't use file as a variable name because it is  command as well.  Try some other name like fileVar
magentoAuthor Commented:

For sample , i gave it as like file .
In original code , it will be my_fname

Gerwin Jansen, EE MVETopic Advisor Commented:
How are you running the script? If I run it I get output.
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

magentoAuthor Commented:

Yes, i even run this piece  of code , getting the output .

But when running via autosys scheduler , it throws empty value.
Looks weird, so need expert assistance

Gerwin Jansen, EE MVETopic Advisor Commented:
Schedulers usually don't know about path settings and environment variables. Check your real script for that and add settings where necessary. Add echo statements that generate a log file, for example: echo "filepath is now: ${filepath}" >> logfile.txt
Dan CraciunIT ConsultantCommented:
Check under what user is the cron/autosys script running. It may be a permission usage.

To test, try logging the result of the ls. It might be empty.

magentoAuthor Commented:
In the log file also its coming empty when i do a echo ...
Its looks like weird issue .Any suggestions.

The file has 777 permission .
magentoAuthor Commented:
Hi Experts,

Please advise how then the debug shows the ls value and then the variable is empty ?

Gerwin Jansen, EE MVETopic Advisor Commented:
- Is the real file path containing spaces? If so, then use ${filepath}
- Is the real path an absolute path? If not, change it to be absolute.
- Show us the first 3 lines of your script.
magentoAuthor Commented:
The issue has been identified.
when i type /bin/ls its working . Since i used as ls its not working.

Any idea why ls is not working?
Gerwin Jansen, EE MVETopic Advisor Commented:
2 options: /bin is not your PATH variable or you have an alias for ls

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
magentoAuthor Commented:
Thanks Gerwin
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
Shell Scripting

From novice to tech pro — start learning today.