Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if - then checks on one line

Status
Not open for further replies.
Feb 12, 2002
80
NO
Hi,

Simple question - I'm tidying up some scripts to make them more "readable" and was trying to change the following code.
All it is meant to do is check that the user has entered valid file names and directory names when running the script from command line.

'm trying to change this:
Code:
  if [ ! -f $INPUT_FILE ] 
  then
   echo " Can not find input file."
   echo " Please check and try again"
   echo ""
   exit
  fi
  
  if [ ! -d $TARGET ]
  then
    echo "Making target directory"
    mkdir $TARGET
    chmod 777 $TARGET
  fi
  
  if [ ! -w $TARGET ]
  then
   echo " Can not write to target directory."
   echo " Please change permissions"
   echo " Exiting ..."
   echo ""
   exit
  fi

to this:
Code:
  if [ ! -f $INPUT_FILE  ] then; echo " Can not find input file. \n Please check and try again \n"; exit; fi
  if [ ! -d $TARGET      ] then; echo " Making target directory \n"; mkdir $TARGET; chmod 777 $TARGET;fi
  if [ ! -w $TARGET    ] then; echo " Can not write to target directory.\n "; exit; fi

(Open your screen up if it doesn not look neat and tidy!)


This does not work.
Any ideas on how I might make it work?
Any ideas on other, better ways of checking things like this?

Thanks,
lil'
 
For me the syntax is:
if [ ... ][!];[/!] then ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

Or more simple:
Code:
  [ ! -f $INPUT_FILE  ] && echo " Can not find input file. \n Please check and try again \n"; exit;
  [ ! -d $TARGET      ] && echo " Making target directory \n"; mkdir $TARGET; chmod 777 $TARGET;
  [ ! -w $TARGET    ] && echo " Can not write to target directory.\n "; exit;
[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Or possibly
Code:
[[ -f $INPUT_FILE  ]] || { echo " Can not find input file. \n Please check and try again \n"; exit; }
[[ -d $TARGET ]] || { echo " Making target directory \n"; mkdir $TARGET; chmod 777 $TARGET; }
[[ -w $TARGET  ]] || { echo " Can not write to target directory.\n "; exit; }

When grouping commands in braces make sure that the last semi-colon has a space between it and the end brace.

Ceci n'est pas une signature
Columb Healy
 
You'd need the braces like in Columb's post

in

Code:
  [ ! -f $INPUT_FILE  ] && echo " Can not find input file. \n Please check and try again \n"; exit;

the exit would be done regardless of the test...

so it would need to be

Code:
  [ ! -f $INPUT_FILE  ] && { echo " Can not find input file. \n Please check and try again \n"; exit; }


not sure about the space needed between ; and last endbrace , but there needs to be space between beginbrace and first command

HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top