Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

test - if [[ .........]] failure 2

Status
Not open for further replies.

dickiebird

Programmer
Feb 14, 2002
758
0
0
GB
Hi
Mental breakdown morning !
I'm trying to test for the presence of 8 files minimum
in current directory, with names of :
RTSBuybacks.0402260909
RTSCountries.0402260909
RTSLoyaltyCardPointBandHistory.0402260909
RTSLoyaltyCardScheme.0402260909
RTSLoyaltyCardTypes.0402260909
RTSSite.0402260909
RTSTransactionLines.0402260909
RTSTransactions.0402260909

This snippet fails with "Doh" - any ideas why ?
Or is there a better way to see if the 8 files exist ?

if [[ -a RTSSite* && -a RTSCountr* && -a RTSBuyb* && -a RTSLoyaltyCardT* && -a RTSLoyaltyCardP* && -a RTSLoyaltyCardS* && -a RTSTransactions* && -a RTSTransactionL* ]]
then
export DS=$(echo RTSTransactions*|cut -c17-26)
else
echo "Doh"
fi


TIA

Dickie Bird (:)-)))
 
It seems that ksh does not make filename generation within [[ ]] (for verification, add 'set -x' before the 'if')

Try this:

[tt]
if [ $(ls -1 RTSSite* RTSCountr* RTSBuyb* RTSLoyaltyCardT* RTSLoyaltyCardP* RTSLoyaltyCardS* RTSTransactions* RTSTransactionL 2>&1 | wc -l) -eq 8 ]]
then
export DS=$(echo RTSTransactions*|cut -c17-26)
else
echo "Doh"
fi
[/tt]



Jean Pierre.
 
Yeah - It seems that ksh doesn't want to expand filenames within [[ ]]
I used your ls -1 suggestion - thanks, J-P

Dickie Bird (:)-)))
 
You could check the return code from ls ...
[tt]
ls RTSSite* RTSCountr* RTSBuyb* RTSLoyaltyCardT* RTSLoyaltyCardP* RTSLoyaltyCardS* RTSTransactions* RTSTransactionL* >/dev/null 2>/tmp/err_msg
if [ $? -eq 0 ]
then
export DS=$(echo RTSTransactions*|cut -c17-26)
else
echo "Doh"
cat /tmp/err_msg
fi
[/tt]
PS. I think Jean Pierre's solution won't work because standard error lines like "RTSSite* not found" will still be counted.
 
Oops..., replace 2>&1 by 2>/dev/null in my test ...
Ygor method is best.


Jean Pierre.
 
Jean-Pierre's solution is fine - I replaced 2>&1 with 2>/dev/null and I must check for 8 files only.
Thanks anyway Ygor.

Dickie Bird (:)-)))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top