LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   weird thing is happening for -f switch (https://www.linuxquestions.org/questions/linux-newbie-8/weird-thing-is-happening-for-f-switch-931439/)

sysmicuser 02-26-2012 07:00 PM

weird thing is happening for -f switch
 
Hallo Folks,

I am having issue with -f switch for conditional if statement.

export TAR_FILENAME=${CCB_ENVNAME}_PRE_${RELEASE}_UPGRADE_`hostname -s`.tar.gz

if [[ -f "${TAR_FILENAME}" ]];then
echo "Application tar ball exist please check backup directory"
exit 1
fi

this one doesn't work if file is present it should fail, but it overwrites existing file, any problemo in script?

Please assist.

Many thanks.

TKH 02-26-2012 07:08 PM

what is the output?
anyway, have you install perl?

chrism01 02-26-2012 07:15 PM

Try this
Code:

#!/bin/bash

# this shows what script is really doing
set -xv

export TAR_FILENAME=${CCB_ENVNAME}_PRE_${RELEASE}_UPGRADE_`hostname -s`.tar.gz

if [[ -f "${TAR_FILENAME}" ]]
then
    echo "Application tar ball exist please check backup directory"
    exit 1
fi

Does filename have a space in it?

@TKH: this is shell, not Perl

sysmicuser 02-26-2012 07:19 PM

No file name doesnot has a space in it.
I shall try iv option alongwith -x

Shall update soon.

Cheers

sysmicuser 02-26-2012 07:25 PM

@TKH

as chirsm01 says it is bash and not perl :)

sysmicuser 02-26-2012 07:28 PM

@chrism01
in meanwhile can you please xplain me what is the difference and what is correct style?

{code}
if [[ -f "${TAR_FILENAME}" ]]
then
echo "Application tar ball exist please check backup directory"
exit 1
fi

{code}

OR
{code}
if [[ -f "${TAR_FILENAME}" ]];then
echo "Application tar ball exist please check backup directory"
exit 1
fi
{code}

What is the correct syntax?

chrism01 02-26-2012 07:35 PM

Technically either is allowed, but I prefer mine as being easier to read, including indenting :)

You might find these useful
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

PS code tags howto https://www.linuxquestions.org/quest...do=bbcode#code for posting on LQ

sysmicuser 02-26-2012 07:41 PM

My bad I was using confluence standard on forum! :(

Thanks for that.

Ran script twice still cannot understand why the heck it is not working :(

Code:

echo $TAR_FILENAME
+ echo BLD02CCA_PRE_1.9.0.6.6.12_UPGRADE_tmelbld12.tar.gz

if [[ -f "${TAR_FILENAME}" ]]
then
        echo "Application tar ball exist please check backup directory"
        exit 1
fi
+ [[ -f BLD02CCA_PRE_1.9.0.6.6.12_UPGRADE_tmelbld12.tar.gz ]]

# ***************************************************************
# *                                                            *
# *    Create Tarball file and put it in the backup directory  *
# *                                                            *
# ***************************************************************

Since the file is generated programmatically I cannot any reason the if -f should fail :(:(

sysmicuser 02-26-2012 07:43 PM

Would my check condition change for a .tar.gz file.

The manual says if [[ -f file ]] true for a regualr file not sure if that .tar.gz is a regular file?

sysmicuser 02-26-2012 07:45 PM

Code:

file BLD02CCA_PRE_1.9.0.6.6.12_UPGRADE_tmelbld12.tar.gz
PRD01CCA_PRE_1906612UPGRADE-tcolpdapp01.transurban.com.au.tar.gz: gzip compressed data, from Unix, last modified: Sun Feb 26 09:33:43 2012

Is that the case that -f switch is not working?

sysmicuser 02-26-2012 07:53 PM

moving slowly and steadily.

noclobber variable may give me answer:)

sysmicuser 02-26-2012 07:57 PM

still close

Code:

set -o
allexport      off
braceexpand    on
emacs          on
errexit        off
errtrace        off
functrace      off
hashall        on
histexpand      on
history        on
ignoreeof      off
interactive-comments    on
keyword        off
monitor        on
noclobber      off
noexec          off
noglob          off
nolog          off
notify          off
nounset        off
onecmd          off
physical        off
pipefail        off
posix          off
privileged      off
verbose        off
vi              off
xtrace          off

if this one works, I would be happy as I have learnt something today ! :):)

sysmicuser 02-26-2012 09:40 PM

Now I am wondering what role does if [[ -f ] does if we can achieve same by set -o noclobber?

I commented out if loop and just set, set -o noclobber and it works fantatstic.

Still not satisfied why -f is not working................

catkin 02-26-2012 10:01 PM

Perhaps the file is not a regular file. You could test by
Code:

file "${TAR_FILENAME}"
if [[ -f "${TAR_FILENAME}" ]]
then
...

BTW, the { and } in "${TAR_FILENAME}" are not wrong but they are not required.

sysmicuser 02-26-2012 11:17 PM

@catkin yes the file is not regular file.
It says
gzip compressed data

how can I test then?


All times are GMT -5. The time now is 05:50 PM.