Find the answer to your Linux question:
Results 1 to 5 of 5
Hello, We are in the process of converting from HP Unix to Linux (bash), and one of our shell scripts is giving an error on the first line of the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2011
    Location
    Pittsburgh, PA, USA
    Posts
    2

    Batch scripting error after Linux conversion


    Hello,

    We are in the process of converting from HP Unix to Linux (bash), and one of our shell scripts is giving an error on the first line of the code below.

    Code:
    if [ ! -f ${FILENAM}* ]
    
    then
     error_out "${FILENAM} is not found" 1 $LINENO
    fi
    Where FILENAM is the first script parameter.

    Here is the error we're getting:

    line 42: [: too many arguments
    Do you know how the if-clause needs to be rewritten to avoid the error? I tried searching online, but have not found a satisfactory solution.

    Thank you,
    Art

  2. #2
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    Code:
    if [ ! -f ${FILENAM}* ];
    then
     error_out "${FILENAM} is not found" 1 $LINENO
    fi
    Cheers

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    It looks to me like Kloscussel's solution simply adds a semicolon to the end of your if statement. I do not believe that is the problem.

    The problem is that you are using a glob in the if. Let us imagine that $FILENAM contains the value "foo". If you are running this script in a directory that contains files "foo", "foobar", and "foobaz", then this line:
    Code:
    if [ ! -f ${FILENAM}* ]
    gets expanded by Bash into:
    Code:
    if [ ! -f foo foobar foobaz ]
    And this is invalid Bash code, because "-f" can only check a single file.

    I don't know what you're trying to do here, but if $FILENAM contains the exact file and should not be globbed, you can just do this:
    Code:
    if [ ! -f "$FILENAM" ]

  4. #4
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    Oh. Looks like I did not read the first message long enough to see the error, sorry.

    If it would have to be really a <file>*, then something like this should work around the symptom:

    Code:
    for i in ${FILENAM}*;
    do
      if [ ! -f  "$i" ];
      then
        error_out "$i is not found" 1 $LINENO
      fi
    done;
    Cheers

  5. #5
    Just Joined!
    Join Date
    Oct 2011
    Location
    Pittsburgh, PA, USA
    Posts
    2
    That worked! Thanks a lot!

Posting Permissions

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