Find the answer to your Linux question:
Results 1 to 7 of 7
I'm testing my script on a 32-bit and 64-bit Linux Mint 11 Virtual Box and I can't actually get the script to go to the end. The problem I'm having ...
  1. #1
    Just Joined!
    Join Date
    Jun 2011
    Posts
    10

    [SOLVED] New to Bash, can't get Bash script to work (Unary error)

    I'm testing my script on a 32-bit and 64-bit Linux Mint 11 Virtual Box and I can't actually get the script to go to the end. The problem I'm having is with the If statement. I've tried an If, Elif, Else statement which actually had the script complete but with extra brackets in it that were annoying. So my friend suggested I tried going to If statements (but she doesn't use this language so it's difficult for her to proofread). So I'm getting the error for the if statement (says line 6: [: =: unary operator expected) and I'm pretty much at a loss for how to get this started (or if the 32 bit is functioning correctly)

    Code:
    #!/bin/bash
    
    #Check if 32- or 64-bit OS, install ia32-libs for dependency.
    d=ia32-libs
    
    if [ `getconf LONG_BIT` = "64" ] && [ `dpkg-query -f'${Status}' --show $d` = "No packages found matching ia32-libs." ] ;
    
    then
    	#echo "$d not detected. Attempting to install it now."
    	apt-get install $d
    	#apt-get --force-yes -yqq install $d
    
    else
    	echo "Message 2, no install required."
    
    fi
    Last edited by tahl; 06-13-2011 at 12:02 PM. Reason: Marked as solved

  2. #2
    Linux Newbie
    Join Date
    Dec 2009
    Posts
    241
    Hi,
    I've also always some problems with bash (still not used to it)

    I assume that the problem is that you run commands to get the string back.
    maybe
    Code:
    if [ ${getconf LONG_BIT} = "64" ] && [ ${dpkg-query -f'${Status}' --show $d} = "No packages found matching ia32-libs." ] ;
    works.
    Can't test it at the moment, but I guess the syntax was something like that.

  3. #3
    Just Joined!
    Join Date
    Jun 2011
    Posts
    10
    That is leaving me with a [: Too many arguments error now. I'm not exactly sure how to interpret the error messages since I've only been doing this a few days and I don't see the error message listed in the manual for Bash.

  4. #4
    Linux Newbie
    Join Date
    Dec 2009
    Posts
    241
    Ok, just tried a little on the console myself ...

    I did use the wrong { ...
    Code:
    if [ "$(getconf LONG_BIT)" = "64" ] && [ "$(dpkg-query -f'${Status}' --show $d)" = "No packages found matching ia32-libs." ] ;
    () tells to run the command in a sub-shell and use the return value while
    {} would run in the same shell ... still don't get why a sub-shell is needed, but it seems to be necessary
    $ use it as variable and the
    " that it has to be interpreted as one string.

    ${Status} just found out is nearly the same as $Status, just that the {} seems to make sure that bash knows where the name starts and ends ...

    that's at least what I believe ...

    Further info:
    http://www.cs.sunysb.edu/documentation/bash/bash.html

  5. #5
    Just Joined!
    Join Date
    Jun 2011
    Posts
    10
    I don't know why I can't get that script working with the && statement in it. I decided to go back to the one I was working on previously but it's giving me an error that is bothering me. For starters I'm not really sure where to put that bracket in red, I thought I had to put it after the apt-get, but if I do then apt-get tries to download packagename ]. Also for the script to function properly, the [ before the elif is required too or it skips that step too. I still think there is a problem with the dpkg though, but it does go to the download. It just never goes to the else if it exists.

    Code:
    #!/bin/bash
    
    #Check if 32- or 64-bit OS
    d=ia32-libs
    
    if [[ `getconf LONG_BIT` = "64" ]]; 
    
    then
    	echo "64-bit operating system detected.  Checking to see if $d is installed."
    
    [ elif [ $(dpkg-query -f'${Status}' --show $d 2>/dev/null) = *\ $d ]] then  
    	echo "$d not detected.  Attempting to install it now."
    	apt-get install $d
    	#apt-get --force-yes -yqq install $d
    else
    	echo "32-bit operating system detected.  Skipping."
    ]
    fi

  6. #6
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    There are a number of problems here. The most glaring one, to me, is your if/elif/else structure. You say that you need a '[' before the elif for it to work properly: this is not true. '[' and ']' are used to enclose a condition for an if or elif statement, not for any sort of control flow. I think that the real problem here is the flow that you've implemented.

    The flow that you have implemented says:

    Code:
    - If `getconf LONG_BIT` = 64
      - Do nothing
    - Otheriwse, if $(dpkg-query -f '${Status}' -- show ia32-libs 2> /dev/null) matches "* ia32-libs"
      - Attempt to install it
    - Otherwise, 32-bit OS
    I don't think this is what you want. I think you want this:
    Code:
    - If `getconf LONG_BIT` = 64
      - If $(dpkg-query -f '${Status}' -- show ia32-libs 2> /dev/null) matches "* ia32-libs"
        - Attempt to install it
    - Otherwise, 32-bit OS
    To implement this second one is:
    Code:
    #!/bin/bash
    
    #Check if 32- or 64-bit OS
    d=ia32-libs
    
    if [[ `getconf LONG_BIT` = "64" ]]; 
    
    then
        echo "64-bit operating system detected.  Checking to see if $d is installed."
    
        if [ $(dpkg-query -f'${Status}' --show $d 2>/dev/null) = *\ $d ]; then
        	echo "$d not detected.  Attempting to install it now."
        	apt-get install $d
        	#apt-get --force-yes -yqq install $d
        fi
    else
    	echo "32-bit operating system detected.  Skipping."
    fi
    Notice how I have nested a new if inside of the body of the first if. This is completely legal.

    You also forgot a semicolon between the nested if condition and its following "then". I have added it in the above code.
    DISTRO=Arch
    Registered Linux User #388732

  7. #7
    Just Joined!
    Join Date
    Jun 2011
    Posts
    10
    Code:
    #!/bin/bash
    
    #This script will determine if the operating system is 32 or 64-bit and then install ia32-libs if necessary.
    d=ia32-libs
    
    if [[ `getconf LONG_BIT` = "64" ]]; 
    
    then
        echo "64-bit operating system detected.  Checking to see if $d is installed."
    
        if [[ $(dpkg-query -f'${Status}' --show $d 2>/dev/null) = *\ installed ]]; then
        	echo "$d already installed."
        else
            echo "Installing now..."
        	apt-get --force-yes -yqq install $d
        fi
    else
    	echo "32-bit operating system detected.  Skipping."
    fi
    I finally figured it out. The above is the correct code to check if the ia32-libs package is installed on a 64-bit operating system. It was much harder to match the tag for a package being uninstalled then it being installed. So it always ended up failing the second If, so I changed the else to install the package and the then to just end it. I tested it back and forth by having the "then" uninstall the package with the else reinstalling it. Thanks for the help both of you, I really appreciate.

Posting Permissions

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