Find the answer to your Linux question:
Results 1 to 2 of 2
I have a function that checks to see if a file is open or closed. Code: function check_file_status { if [ -f "$1" ]; then LSOF=$(lsof "$1") else unset LSOF ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! pendal's Avatar
    Join Date
    Jan 2008
    Posts
    15

    BASH .. if[] not working ..help out!


    I have a function that checks to see if a file is open or closed.
    Code:
    function check_file_status {
    
    if  [ -f "$1" ]; then
    	LSOF=$(lsof "$1")
    else
     	unset LSOF
    fi
    
    if [ -n "$LSOF" ] ; then
    	echo "open"
    else
    	echo "closed"
    fi
    }
    I am trying to use that in another function within my bash script.

    Code:
    			if [$(check_file_status "$1$item") == 'open' ]; then
    				echo "file is open"					
    			else
    				echo "$1$item is close"	
    			fi
    when I run the script though i get the following error

    Code:
    ftp_monitor.sh: line 77: [closed: command not found
    if i run with -x i see the following
    Code:
    ++ echo closed
    + '[closed' == open ']'
    ftp_monitor.sh: line 77: [closed: command not found

    i have also tried the following in the if statment
    Code:
    if ["$(check_file_status "$1$item")" == "open" ];
    and get the same results.

    can anyone shed some light on what's happening?

    Thanks for any help

    C
    Last edited by pendal; 11-10-2010 at 06:36 PM. Reason: title change

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,286
    Hi.

    Look carefully at the error message, noting that characters in:
    Code:
    [closed
    are all together.

    Use whitespace around "[" and "]".

    The "[" is actually a command, namely test.

    See man bash to learn more ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •