Find the answer to your Linux question:
Results 1 to 2 of 2
First of all, I know there is probably an easier way to do what I'm doing, i'm just playing around trying to learn some things. I don't necessarily want this ...
  1. #1
    Just Joined!
    Join Date
    Mar 2007
    Posts
    15

    Perplexed By Error - Bash

    First of all, I know there is probably an easier way to do what I'm doing, i'm just playing around trying to learn some things. I don't necessarily want this to work I'm just really confused by the error here. I DONT WANT SOLUTIONS, I WANT REASONS

    This is just a little script i tried to write to grab values from config files.
    Here's the error first, then the config file and code is below.

    Code:
    jeremy:/usr/local/bin# ./getconfiguration /usr/local/etc/medman.config MEDIADIR
    ./getconfiguration: line 25: =/home/media/: No such file or directory

    i see that it's looking for "=/home/media" (with the equals sign) but from what i see there is no call to anything that should be looking that directory. That's the value i'm trying to extract from the config file.


    if have a config file

    Code:
    #/usr/local/etc/medman.config
    MEDIADIR=/home/media
    And here's the script (you can probably skip the error checking)

    usage: getconfiguration <path to config file> <name of value to extract>

    Code:
    #!/bin/bash
    
    #$1 config file         #i know i should rename these but i'm not trying to fix this script, just understand it
    #$2 variable name
    
    ##########################ERROR CHECKING#########################
    if [ -z $1 ]
    then
            echo "Not enough arguments"
            exit 1
    fi
    if [ -z $2 ]
    then
            echo "Not enough arguments"
            exit 1
    fi
    
    if [ ! -f $1 ]
    then
            echo "No such file \"$1\""
            exit 2
    fi
    ############################END ERROR CHECKING######################
    
    
    $val=$(grep "$2=*" $1 | cut -d '=' -f 2)   #this is the line that produces the error (line 25)
    
    echo $val                                           #not sure the best way to return this value but that's not the problem
    
    exit 0


    EDIT: Also what is the best way to return values from (what will be) a child process without printing them to the screen. the only way i really know how to do this is VAR=$(somecommand)
    Last edited by jbarnes8; 12-10-2007 at 11:08 PM. Reason: Kill 2 birds with one stone

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Alrighty. So here's what I think is happening:

    The error is being caused by using "$val=", rather than "val=". "$val" means "the value of the val variable". Because val is unset, $val is nothing, so the line becomes:
    Code:
    =$(grep "$2=*" $1 | cut -d '=' -f 2)
    And then, of course, the grep command returns "/home/media", resulting in the line reading:
    Code:
    =/home/media
    Because this line is not anything special in Bash (it is not an assignment, conditional, loop, function, etc.), it assumes that this is a command you want to execute (particularly, there should be a directory "=" that contains a directory "home" that contains an executable file called "media"). Obviously, because this does not exist, you are getting this error.

    I will also note that the regular expression being used in the grep statement is wrong. The way you have it, you are looking for the variable, followed by 0 or more equals signs. I suggest either removing the '*', or adding that you are looking for 0 or more characters after the '=' (which would require the addition of a "[a-zA-Z]".
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

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