For loop - unexpected token `do

My requirement is to search for current date-1 .log files in /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs folder and write the file name to filenames.txt

When I ran the script below, I got error as

syntax error near unexpected token `do

I'm not sure what is wrong in my code. I seek some help to achieve this task effeftively

#!/bin/sh
for i in /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/"SASApp_STPServer_"$(date +%Y-%m-%d --date "1 day ago")"_tmptcmsaslva2_"*".log"
do
  echo $i
done

echo $i >> output_file.txt

Hello Ram_kumar_BE,

Could you please try following and let me know if this helps.

#!/bin/sh
for i in "/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_"$(date +%Y-%m-%d --date "1 day ago")"_tmptcmsaslva2_"*".log"
do
    echo $i
done
 

If you are happy with above command's result then please change echo $i to echo $i >> output_file.txt

Thanks,
R. Singh

The way /bin/sh processes command substitution and the way date processes --date varies considerably from operating system to operating system.

What operating system are you using?

You said your output filename is filenames.txt , but nothing in your script writes anything to a file with that name???

Hi Ravinder,

Still I'm getting the same error.

---------- Post updated at 01:14 AM ---------- Previous update was at 01:11 AM ----------

I'm using SSH secure client to connect the terminal. Bash version is 4.1.3.

My output filename is output_file.txt.

I repeat: "What operating system are you using?" And, since you're using ssh , show us the exact ssh command you're executing and tell us what operating system you're using on the system where you're running ssh and what operating system you're using on the system you're connecting to with ssh .

I will execute the script in command prompt like

sh list_file.sh

I use SSH secure client which is a windows application and then select the server which I need to connect by supplying username and password.

Then I will navigate to 'New terminal window' from the tool bar in SSH secure client and then I will go to the directories to develop\run a shell script.

If you need any further information about my environment, kindly advise me the steps which you want me to look for.

Thanks.

---------- Post updated at 02:27 AM ---------- Previous update was at 01:43 AM ----------

My operating system is LINUX

After you connect to the server and open a new terminal window, show us the output from the following commands typed into that terminal window:

/bin/sh --version
uname -a
type bash date sh
ls -l /bin/bash /bin/date /bin/sh

PS... And what output do you get from the command:

date +%Y-%m-%d --date "1 day ago"
-bash-4.1$ uname -a
Linux serername  x86_64 x86_64 GNU/Linux
-bash-4.1$ type bash date sh
bash is /bin/bash
date is /bin/date
sh is /bin/sh
-bash-4.1$ ls -l /bin/bash /bin/date /bin/sh
-rwxr-xr-x 1 root root 938832 Jul  9  2014 /bin/bash
-rwxr-xr-x 1 root root  59392 Oct 17  2014 /bin/date
lrwxrwxrwx 1 root root      4 Mar  4  2015 /bin/sh -> bash
-bash-4.1$ 
-bash-4.1$ date +%Y-%m-%d --date "1 day ago"
2015-08-27
-bash-4.1$ 

[/CODE]

---------- Post updated at 04:34 AM ---------- Previous update was at 03:45 AM ----------

I managed to get output now. THere was a line feed issue earlier.

My output file (output_file.txt) Contains filenames alongwitH direCtories like below.

/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-27_tmptcmsaslva2_19142.log

HoweVer I need only tHe filename. How to do tHat?

I also need a log file for te sript exCeution. wHat would be tHe Command for tHat?

Use bash 's parameter expansion / Remove matching prefix pattern: ${parameter##word}

What do you want to log (entire execution / all stdout/stderr / extra logging output)?

I want tHe Command to Capture entire exeCution in log.

Kindly giVe Command witH example to remoVe matCHing prefix as I'm not sure write a Command wit your inputs.

THanks.

Why are so many letters within a word in caps? (your first posts were good)
Please try to write proper grammer -> Nobody is perfect and many here are not native english speakers/writers/readers -> and i know of no language having capital letter within a word.

If that happens by accident, please clean your keyboard.

Thank you.

To just produce the filenames, try:

cd /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs
for i in "SASApp_STPServer_$(date +%Y-%m-%d --date "1 day ago")_tmptcmsaslva2_"*.log
do	echo "$i"
done > output_file.txt

or, assuming that your list of files won't exceed ARG_MAX limits, just:

cd /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs
printf '%s\n' "SASApp_STPServer_$(date +%Y-%m-%d --date "1 day ago")_tmptcmsaslva2_"*.log > output_file.txt