0

I'm trying to write a program in bash shell which handles a file. I have to call my program from the command line with various ways so they can print specific columns each time. Let's give for example this call:

./prog.sh -f file

my code for this part is

if [[( $1 == '-f') && ( $2 == '<file>')]] ; then 
 echo "do stuff and print column"

I do get the column I want printed, however I'm also getting the following warning:

[-f command was not found

Any idea on how to remove the error?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 4
    Please take a look: http://www.shellcheck.net/ – Cyrus Oct 10 '17 at 18:14
  • I can't reproduce that error message with that code (on GNU Bash 4.3). Are you sure that that's the part of your code that's causing the error? – wjandrea Oct 10 '17 at 18:22
  • The specific line you gave doesn't cause the error, *but* despite the lack of a working [mcve], it's an error with a very-well-understood cause, for which we have numerous duplicative, already-answered questions. – Charles Duffy Oct 10 '17 at 19:38

2 Answers2

0

Make sure there's space around the square brackets. [[ and ]] need to be separate tokens. The parentheses don't add anything and can be deleted. (If you leave them they also need spaces on both sides.)

if [[ $1 == '-f' && $2 == '<file>' ]]; then 
    echo "do stuff and print column"
fi

For what it's worth, the $2 check probably shouldn't be there. Any string is a valid file name.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
-2

You should rewrite the file check as a test so your code would be:

if [[( -f $1 ) && ( $2 == '<file>')]] ; then 
   echo "do stuff and print column"