Results 1 to 6 of 6

Thread: super easy BASH null if command line args question

  1. #1
    Join Date
    Dec 2010
    Location
    Earth
    Beans
    151
    Distro
    Ubuntu 14.04 Trusty Tahr

    super easy BASH null if command line args question

    grrr i get error while checking for null in $1 command line arg[] number 1

    Code:
    #!/bin/bash
    # just search my movie dir for a movie, use grep list all so i only have to have
    # a part or 1 whole word of title to show the movie
    # why: I forget what's in there  
    
    dir1="/storage/Movies"
    dir2="/storage/Movies/NewMovies"
    
    #make sure search is not empty - null
    if [ $1 !=  0 ]; then
     # bash check if dir1 exists
     if [ -d $dir1 ]; then
       echo ... $dir1 ...
       #list dir and grep searchname
       ls  $dir1 | grep $1
     fi
     #in line no while just two dir  
     if [ -d $dir2 ]; then
       echo ... $dir2 ...
       ls $dir2 | grep $1
     fi
    else
     echo usage: $0 moviename
     echo note moviename can be any part of the whole title   
    fi
    i get
    line 10: [: !=: unary operator expected
    if arg1 is left null

    i don't know what to change line to
    Code:
    #make sure search is not empty - null
    if [ $1 !=  0 ]; then

  2. #2
    Join Date
    Nov 2008
    Location
    Boston MetroWest
    Beans
    16,326

    Re: super easy BASH null if command line args question

    Try

    Code:
    if [ "$1" != "" ]

  3. #3
    Join Date
    Dec 2010
    Location
    Earth
    Beans
    151
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: super easy BASH null if command line args question

    Quote Originally Posted by SeijiSensei View Post
    Try

    Code:
    if [ "$1" != "" ]
    thanks works perfect.

    I use the cheesier thru .bash_aliases all time before downloading stuff. fast, easy and kinda cheesy.
    I tried
    if [ $1 != "" ] with out string quotes on the var cmd arg and got the same i didn't think of making the arg in string quotes.

    you rock
    SeijiSensei

  4. #4
    Join Date
    Sep 2010
    Beans
    4

    Re: super easy BASH null if command line args question

    Not sure why I was told this long ago, but I have been following their advice ever since.

    if [ "$1" != "" ]; then

    should be:

    if [ "x$1" != "x" ]; then


    There was a reason for this. Something about if $1 happens to be non-present, and not necessarily just null. Previous case usually works though! the x adds just one more level of redundancy.


    Just my little FYI on the matter,

    cheers!

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: super easy BASH null if command line args question

    another way to skin the cat
    if [ -z "$1" ]; then

  6. #6
    Join Date
    Jun 2008
    Location
    California
    Beans
    2,271
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: super easy BASH null if command line args question

    Quote Originally Posted by jason.m View Post
    Not sure why I was told this long ago, but I have been following their advice ever since.

    if [ "$1" != "" ]; then

    should be:

    if [ "x$1" != "x" ]; then

    [SNIP]
    This whole issue is discussed under item 4 on the following page:

    http://mywiki.wooledge.org/BashPitfa...goryShell\b%29

    It generally recommends that the x$var solution be avoided.

    You may have seen code like this:

    [ x"$foo" = xbar ] # Also right!

    The x"$foo" hack is required for code that must run on ancient shells which lack [[, and have a more primitive [, which gets confused if $foo begins with a -. But you'll get really tired of having to explain that to everyone else.

Tags for this Thread

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
  •