Shell script file check throws [: too many arguments


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Shell script file check throws [: too many arguments
# 1  
Old 02-08-2019
Shell script file check throws [: too many arguments

While I am trying to check the filename/s in IF statement of a shell script (RedHat Linux 6) I am getting below error:

File check:

Code:
filename_time2=`date --date='yesterday' +%Y-%m-%d`
cd /location/of/the/files/to/copy

if [ -f server.log-$filename_time2* ]
then
                cp server.log-$filename_time2* ../archive/new
                echo Successfully copied
else
                echo No server log files present with timestamp $filename_time2
fi

Code:
This throws the error -        [: too many argument

Throws error when there are multiple files in the location. But when there is 1 file match in the location it is working fine.

I have already tried with below options :

Code:
if [ -f "server.log-$filename_time2*" ]
if [[ -f "server.log-$filename_time2*" ]]
if [[ -f server.log-$filename_time2* ]]

All the above cases it is not throwing error but it returns FALSE and executing the else block although the file is present in the location with timestamp

Could you please help me how I can use the if condition here to check whether the files are present in the location.

Thanks.
# 2  
Old 02-08-2019
Maybe something more like:
Code:
filename_time2=$(date --date='yesterday' +%Y-%m-%d)
cd /location/of/the/files/to/copy

for file in server.log-$filename_time2*
do	if [ -f "$file" ]
	then
		cp server.log-$filename_time2* ../archive/new
		exitcode=$?
		echo "One or more files with timestamp $fiename_time2 copied"
		exit $exitcode
	else
		echo "No server log files present with timestamp $filename_time2"
		exit 1
	fi
done

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 02-08-2019
Thanks for your suggestion @Don Cragun.

Actually in my case always there are always some server.log file present with timestamp. So I would always like to check the if statement with timestamp.
I tried after your suggestion like the below:
Code:
filename_time2=$(date --date='yesterday' +%Y-%m-%d)
file=audit.log-$filename_time2*

                if [ -f "$file" ]
                then

                     cp server.log-$filename_time2* ../archive/new
		     exitcode=$?
		     echo "One or more files with timestamp $fiename_time2 copied"
		     exit $exitcode
	         else
		     echo "No server log files present with timestamp $filename_time2"
		     exit 1
	          fi

This is also failed to find the files so the else block is executing.
# 4  
Old 02-08-2019
DonCragun's code as posted would have worked. You modified a crucial part of that code, vis
Code:
for file in server.log-$filename_time2*

This expands the wildcard; the if statement then checks the first file in the list. If that file exists all the files are copied at once and the loop is terminated.
Try the code again as posted in post 2 of this thread rather than modified by you.

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 5  
Old 02-08-2019
Expanding a bit on what Andrew already said...

Did you try the code I suggested in post #2 in this thread?

If so, in what way did it fail?

Are you saying that you have some files that will match the filename matching pattern audit.log-$filename_time2* that are regular files and there are other filenames matching that pattern at the same time that are of some other filetype?

From what you stated in post #1, I assumed that you either had one or regular files that would match that pattern or that there were no existing files that would match that pattern. If that is the case, the code I suggested should have done exactly what you wanted it to do (assuming that you are using a shell that meets a fairly minimal subset of the requirements for the shell command programming language specified by the POSIX standards).

The code that you are trying (calling test (or it alias [) with multiple filename operands or with a quoted filename matching pattern containing wild-card characters cannot ever be convinced to do what you are trying to do if more than one file exists matching that pattern. The test with more than one file operand is always going to give you the diagnostic message you reported seeing in post #1.

If, however, it is always the case that at least one existing filename will match your filename matching pattern, then there is no need for the test command at all and you can just replace your entire script with:
Code:
filename_time2=`date --date='yesterday' +%Y-%m-%d`
cd /location/of/the/files/to/copy
cp server.log-$filename_time2* ../archive/new
exitcode=$?
echo copying attempted
exit $exitcode

but, at least it will give you a non-zero exit code if it failed to copy one or more files successfully.

The $( command ) form of command substitution is preferred over the ` command ` form unless you are using a 1970's Bourne shell that doesn't accept the preferred form.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 02-08-2019
Thank you very much Don Cragun and apmcd47 . I made a mistake while testing the code. The code mentioned by Don Cragun is working perfectly.
Many thanks for your quick help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell Script to check a file

I'm required to write a simple shell script that when it runs it writes the output which is a simple barcode to a tmp flat file which I can do the bit I'm struggling with... The next time it runs I need to check the tmp output file to see if that barcode is in the output file and if it is send... (5 Replies)
Discussion started by: worky
5 Replies

2. UNIX for Beginners Questions & Answers

Passing Arguments to shell script from file is not working as expected.

Hi All, I have below simple shell script in cloudera quick start vm cenos 6 which copy file from source to destination. # file_copy.sh source_dir = ${source_dir} target = ${target_dir} cp source_dir target and my parameter file is like below #parameter_file.txt source_dir =... (4 Replies)
Discussion started by: Narasimhasss
4 Replies

3. UNIX for Dummies Questions & Answers

Need shell script to check file

Hi Experts, I am not good in writing script. Just stared.I am looking for shell script to check following parameters. 1) Number of files on remote Linux SUSE server.- Any directory and sub directory. 2) I should define number of files in script. Files should be variable. 3) Age of... (2 Replies)
Discussion started by: ApmPerfMonitor
2 Replies

4. Shell Programming and Scripting

Calling shell script within awk script throws error

I am getting the following error while passing parameter to a shell script called within awk script. Any idea what's causing this issue and how to ix it ? Thanks sh: -c: line 0: syntax error near unexpected token `newline' sh: -c: line 0: `./billdatecalc.sh ... (10 Replies)
Discussion started by: Sudhakar333
10 Replies

5. UNIX for Dummies Questions & Answers

How to check arguments in shell???

for example I have make target file is optional. So can I check whether there is or no? I tried if test $# -eq 1 then path=$1 else path=$2 fi But it doesnt work properlu ;( Please use code tags next time for your code and data. (12 Replies)
Discussion started by: Manueldo
12 Replies

6. Shell Programming and Scripting

php file unable to run shell script with arguments

echo $result=exec("./permit.sh".$_FILES); pls suggest some other method to run shell script in php .:wall::mad: (0 Replies)
Discussion started by: upvan111
0 Replies

7. Shell Programming and Scripting

Reading arguments for a shell script from file

I have a shell script that takes 2 arguments. I will have to execute this script multiple times with different values for the arguments. for example, ./shscript env1 value1 ./shscript env1 value2 ./shscript env2 value3 ./shscript env3 value4 ./shscript env1 value5 ./shscript env3... (24 Replies)
Discussion started by: goddevil
24 Replies

8. Shell Programming and Scripting

send arguments to a .exe file from a shell script

Folks , can anyone post a sample showing a way to parse a variable containing a string to a .exe file . Thanks Venu (2 Replies)
Discussion started by: venu
2 Replies

9. Shell Programming and Scripting

How to pass arguments to SQL file passed in shell script?

Hi, I am using SYBASE database. in my script i am connecting to DB via using isql. isql -U${S_USER} -S${S_SERV} -D${S_DB} -P${S_PWD} -b0 -w3000 -h0 -s"|" -i${MYDIR}/ABC.sql -oXYZ.txt << FINSQL i am taking a ABC.sql file to use the queries written in it and storing the output in... (3 Replies)
Discussion started by: dazdseg
3 Replies

10. Solaris

Passing arguments to a shell script from file while scheduling in cron

Hi, I have a shell script Scp_1.sh for which I have to pass 2 arguments to run. I have another script Scp_2.sh which in turns calls script Scp_1.sh inside. How do I make Scp_1.sh script to read arguments automatically from a file, while running Scp_2.sh? -- Weblogic Support (4 Replies)
Discussion started by: weblogicsupport
4 Replies
Login or Register to Ask a Question