Test file script - if evaluation failing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Test file script - if evaluation failing
# 1  
Old 06-10-2013
Test file script - if evaluation failing

I have a following script to evaluate if file exist in the directory and then archive it.

Code:
#!/bin/bash
 
#master directory
scriptdir="/flex/sh/interfaces" #change this path only - all other paths are connected with it
filedir="/flex/interfaces" #change this path only - all other paths are connected with it
#files directory
outputdir="${filedir}/correspondence/outgoing"
outputarchdir="${filedir}/correspondence/outgoing_archive"
#files
output_file="${outputdir}/if_flex_Output.All_*"
Archive_file()
{
#rename original output corp correspondence file and move to archive output directory
cd $outputdir
echo $output_file
echo $outputdir
echo $outputarchdir
#mv $output_file $outputarchdir
if [[ -e $output_file ]]; then
mv $output_file $outputarchdir
printf "Original output corp correspondence file archived\n"
else
printf "Unable to archive original output corp correspondence file\n"
exit 1
fi
}
 
Archive_file

I knew the file name before and used it number of times for different files and it worked but never tried to match filename with 'star' at the end like in this line:

Code:
 
output_file="${outputdir}/if_flex_Output.All_*"

Now I don't know exact file name and I think * cause some issues in if evaluation.

File in directory is called 'if_flex_Output.All_20130405_132723"

Every time I run scrip I'm getting 'Unable to archive original output corp correspondence file'

When I moved
Code:
 
mv $output_file $outputarchdir

above if statement it works fine.

Any idea why??
# 2  
Old 06-10-2013
if [ -f ... ] expects one filename. Not three, not zero, but one and only one.
# 3  
Old 06-10-2013
There is only one file that will resolve from
Code:
output_file="${outputdir}/if_flex_Output.All_*"

Corona688 you are right that if I change code to

Code:
if [ -e $output_file ]; then

it is working as expected.

Question is why it is not working with double brackets while working for other files?

When I remove the timestamp from file and look for

Code:
output_file="${outputdir}/if_flex_Output.All_"

if statement with double brackets also works.

I know how to resolve this issue with above workarounds but looking to understand code behaviour with double brackets.

Thanks
# 4  
Old 06-10-2013
Quote:
Originally Posted by viallos
Question is why it is not working with double brackets while working for other files?
Interesting, I wouldn't have expected that, I thought double vs single brackets was incidental.

The double brackets have different behavior of course, but some of their behavior is shared, and I thought file expansion on unquoted variables was one of them...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Evaluation of test command

Could somebody please explain to me why and how the highlighted line(s) (?) of code puts the "test" evaluation into "result" and then to $enable_static ? Or does not ? I did comment out the original code and changed it to what I feel is less cryptic , but the "result" is still wrong =... (3 Replies)
Discussion started by: anne
3 Replies

2. UNIX for Beginners Questions & Answers

Error with file test script

Dear friends i am getting error when i run below script #!/bin/bash echo "Enter the name of the file" read file_name if then If then echo "add some text to quit ctrld" cat >> $file_name else echo "the file does not have write permission" fi else echo "$file_name does not... (1 Reply)
Discussion started by: Ibrahims1
1 Replies

3. UNIX for Advanced & Expert Users

PERL DBD make test on Linux failing

I am installing Oracle DBD to PERL 5.16.3 and during make test , I am running into this error :rm -f blib/arch/auto/DBD/Oracle/Oracle.so LD_RUN_PATH="/opt/oracle/product/11.2.0/racdb11204/lib" gcc -m32 -shared -O2 -L/usr/local/lib -fstack-protector Oracle.o dbdimp.o oci8.o -o... (3 Replies)
Discussion started by: talashil
3 Replies

4. UNIX for Dummies Questions & Answers

Script test failing

Testing some old script developed by different user. #!/usr/bin/sh case "$0" in */*) cmd="$0";; *) cmd=`which "$0"`;; esac dir=`dirname "$cmd"` node="$dir/." echo $node below two simple tests are failing, I am not seeing any Control+M characters in the script file and I am not able... (4 Replies)
Discussion started by: srimitta
4 Replies

5. Shell Programming and Scripting

modify the test file by any script

Hi All the Helpers! I have a text file which looks like input.txt.I would request to please suggest me how can I make this file look like output.txt input.txt VOP 111 0 1 2 DEM 111 0 222 333 444 555 DEM 879 888 987 888 989 DEM 879 888 987 888 989 VOP 118 0 12 3 6... (7 Replies)
Discussion started by: Indra2011
7 Replies

6. Shell Programming and Scripting

Output redirection of c binary file to a file in shell script is failing

I am struck up with a problem and that is with output redirection. I used all the ways for the redirection of the output of c binary to a file, still it is failing. Here are the different ways which I have used: ./a.out | tee -a /root/tmp.txt 2>&1 ./a.out | tee -a /root/tmp.txt 1>&1 ./a.out |... (2 Replies)
Discussion started by: Maya29988
2 Replies

7. UNIX for Dummies Questions & Answers

File operations are failing inside the script

Hi, does any one know the environmental parameter that I have to set so as to make sure the file operations run properly within the script. right now when I am doing a cat from within the script nothing happens, same is the case when I do a grep. when I am doing awk '{print $0 }' its printing... (1 Reply)
Discussion started by: ahmedwaseem2000
1 Replies

8. Shell Programming and Scripting

Expect script help needed- script failing if router unavailable

Hey all. Sometimes I'm tasked to change some router configs for the entire network (over 3,000 Cisco routers). Most of the time its a global config parameter so its done with a loop and an IP list as its the same configuration change for all routers. This is working OK. However, sometimes an... (3 Replies)
Discussion started by: mrkz1974
3 Replies

9. Shell Programming and Scripting

Display which test in the if/else is failing

So I have a script that monitors my drives (/dev/sda and /dev/sdb) using smartctl (smartmontools). I'm by no means an expert in scripting, so this was my attempt at creating a way to email me if one of the values in smartctl output goes above a set threshold. My question is, I'm trying to edit... (5 Replies)
Discussion started by: bound4h
5 Replies

10. Shell Programming and Scripting

Test File Reading & Validation using Shell script

Please help develop script for below requirement -------Sample file------------------------------- HSVSHOSTRECON 20090115011817BP DARMAR60064966247003504720000000000000000000066626000000000000133000003D003463001332 ... (14 Replies)
Discussion started by: niraj_bhatt
14 Replies
Login or Register to Ask a Question