LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 02-26-2012, 07:00 PM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 247

Rep: Reputation: 0
Unhappy 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.
 
Old 02-26-2012, 07:08 PM   #2
TKH
Member
 
Registered: Jul 2011
Location: Milky Way
Distribution: Ubuntu, LFS, Slackware, Fedora
Posts: 205

Rep: Reputation: 20
what is the output?
anyway, have you install perl?
 
Old 02-26-2012, 07:15 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
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
 
Old 02-26-2012, 07:19 PM   #4
sysmicuser
Member
 
Registered: Mar 2010
Posts: 247

Original Poster
Rep: Reputation: 0
No file name doesnot has a space in it.
I shall try iv option alongwith -x

Shall update soon.

Cheers
 
Old 02-26-2012, 07:25 PM   #5
sysmicuser
Member
 
Registered: Mar 2010
Posts: 247

Original Poster
Rep: Reputation: 0
@TKH

as chirsm01 says it is bash and not perl
 
Old 02-26-2012, 07:28 PM   #6
sysmicuser
Member
 
Registered: Mar 2010
Posts: 247

Original Poster
Rep: Reputation: 0
@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?
 
Old 02-26-2012, 07:35 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
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
 
Old 02-26-2012, 07:41 PM   #8
sysmicuser
Member
 
Registered: Mar 2010
Posts: 247

Original Poster
Rep: Reputation: 0
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
 
Old 02-26-2012, 07:43 PM   #9
sysmicuser
Member
 
Registered: Mar 2010
Posts: 247

Original Poster
Rep: Reputation: 0
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?
 
Old 02-26-2012, 07:45 PM   #10
sysmicuser
Member
 
Registered: Mar 2010
Posts: 247

Original Poster
Rep: Reputation: 0
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?
 
Old 02-26-2012, 07:53 PM   #11
sysmicuser
Member
 
Registered: Mar 2010
Posts: 247

Original Poster
Rep: Reputation: 0
moving slowly and steadily.

noclobber variable may give me answer
 
Old 02-26-2012, 07:57 PM   #12
sysmicuser
Member
 
Registered: Mar 2010
Posts: 247

Original Poster
Rep: Reputation: 0
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 !
 
Old 02-26-2012, 09:40 PM   #13
sysmicuser
Member
 
Registered: Mar 2010
Posts: 247

Original Poster
Rep: Reputation: 0
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................
 
Old 02-26-2012, 10:01 PM   #14
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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.
 
Old 02-26-2012, 11:17 PM   #15
sysmicuser
Member
 
Registered: Mar 2010
Posts: 247

Original Poster
Rep: Reputation: 0
@catkin yes the file is not regular file.
It says
gzip compressed data

how can I test then?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Weird thing happening to my computer after installation dsplayer14 Slackware - Installation 4 11-12-2011 11:26 AM
strange thing happening with touchpad and http links and tabs with FF4 in latest RC vdemuth Slackware 2 03-30-2011 08:58 AM
Weird thing happening with graphics after update MTK358 Linux - General 2 04-11-2010 08:35 AM
Weird thing happening with GTK apps lancelott *BSD 0 06-22-2004 03:27 PM
Weird things are happening saiz66 Slackware 8 05-17-2003 12:03 AM


All times are GMT -5. The time now is 03:35 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration