Results 1 to 8 of 8

Thread: If statement with empty string

  1. #1
    Join Date
    Feb 2013
    Beans
    25

    If statement with empty string

    Hi there,

    I got a problem with a little If statement and Empty sting here.

    The Idea is to check for the Graphic Card so I can select an Installation-package for the right Video Drivers (Intel, ATI, NVIDIA,...)

    My If statement shall determine if the string I am looking for exists or not

    This is my Trial and Error Code:
    Code:
    #! /bin/sh
    if [ `lspci | grep -i vga | grep -i nvidia` = "" ]; then
        echo "off"
    else   
        echo "on"
    fi
    And i get always the same result no matter what I'm looking for, or on which Computer I'm testing it:
    Code:
    -su: [: =: unary operator expected
    on
    Hope some of you can help me
    THX in advance
    Last edited by lisati; February 20th, 2013 at 12:55 PM. Reason: added [code] and [/code] tags to aid readability

  2. #2
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: If statement with empty string

    Quote it
    Code:
    if [ -z "`lspci | grep -i vga | grep -i nvidia`" ]; then
    or, better yet, avoid the test command althogether
    Code:
    if lspci | grep -qi 'vga .* nvidia'; then
      echo on
    else
      echo off
    fi
    Last edited by schragge; February 20th, 2013 at 11:49 AM.

  3. #3
    Join Date
    Feb 2013
    Beans
    25

    Re: If statement with empty string

    Still getting
    -su: [: too many arguments
    on


    on the wrong system

  4. #4
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: If statement with empty string

    Can you please post the command you're trying now? And what about getting rid of test? I've updated my previous post.

  5. #5
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: If statement with empty string

    +1 "quote it"

    I think it is easier to see what is happening when you write the script like this:

    Code:
    #!/bin/bash
    if [ "$(lspci | grep -i vga | grep -i nvidia)" == "" ]
    then
     echo "off"
    else 
     echo "on"
    fi
    Edit: The if [ ] syntax is for bash and will not work in sh
    Last edited by sudodus; February 20th, 2013 at 12:03 PM.

  6. #6
    Join Date
    Feb 2013
    Beans
    25

    Re: If statement with empty string

    Thx I'll try that

    what did you mean with the test command ?

    I'm really new to this stuff ^^

  7. #7
    Join Date
    Feb 2013
    Beans
    25

    Re: If statement with empty string

    Hahaa! It seems to work, with the last one, thank you very much for your help, kind Sirs.
    Last edited by thedawg; February 20th, 2013 at 01:05 PM.

  8. #8
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    8,753
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: If statement with empty string

    If you're testing the result of a command with $(), you need to wrap that in quotes as you just did:

    Code:
    if [ "$(some command)" = "" ]
    If you ask for help, please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

Bookmarks

Posting Permissions

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