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:
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 :
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.
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:
This is also failed to find the files so the else block is executing.
DonCragun's code as posted would have worked. You modified a crucial part of that code, vis
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.
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:
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:
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.
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)
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)
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)
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)
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)
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)
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)
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)