0

I am having a lot of trouble using a command line argument in a script file to check if a file is being entered in the command line. So basically I have a script file named ./file and a logfile named log. I have an if then else statement to run a check to see if the right file is entered and if not the program exits with an echo error. If the right file name is entered then it continues to run script. I either get an error directory or file doesn't exist or my echo error no matter what I do. Can someone please point out what I'm doing wrong? Thank you for any help provided. I am using putty. this is what I have

  if ["./file log"]
      then
            echo ""
  else
            echo "error"
            exit 0
  fi
  • Are you saying that you should run the script with $ ./file log, i.e. with log as the argument? Your script seems to reference ./file instead of log? – Sparhawk Nov 15 '15 at 4:26
  • Yes I'm sorry I changed it – jake Nov 15 '15 at 4:27
  • that isn't unix shell syntax. are you trying to write a unix shell script? – mikeserv Nov 15 '15 at 4:28
  • I'm using vi linux – jake Nov 15 '15 at 4:29
  • 3
    if there's only one "right file" possible. why even take an argument? – cas Nov 15 '15 at 4:31
1

You don't need to (self-)reference the script itself. You know it must exist. You just need to see if the log file exists. Hence, use the -f test ("FILE exists and is a regular file"). See man test for more information (test is essentially equivalent to [...]).

if [ -f "log" ]
  then
    echo ""
  else
    echo "error"
    exit 0
fi

If the log file is to provided by the argument when calling the script (e.g. $ ./file log), then you can refer to the first argument with $1. Hence,

if [ -f "$1" ]
  • WOW that was it. I feel like a complete idiot. Thank you!! If I may ask what is the $1 stand for??? – jake Nov 15 '15 at 4:33
  • You can refer to the each provided argument with $1, $2, etc. in order. You can also refer to all with $@ and find the number of provided arguments with $#, which might both be useful for error-checking. – Sparhawk Nov 15 '15 at 4:35
  • I can't thank you enough my head is hung in shame – jake Nov 15 '15 at 4:36
  • 2
    We all had to start somewhere. It might be worth you seeking out some basic scripting resources though. These are relatively basic constructions in *nix land. Good luck. – Sparhawk Nov 15 '15 at 4:38
1

You must want this too. $0 is the name of the script, and $# the number of arguments. Additional notes, by default a script returns 0 on success, and <> 0 on error

#!/bin/bash 
    display_usage() { 
        echo "This script must be run with ...." 
        echo -e "\nUsage:\n$0 [arguments] \n" 
    } 

if [ $# -eq 0 ]; then
    display_usage
    exit 1
fi
.... script ....
exit 0
  • 1
    Yup. And then you'll want to run sanity checks on the arguments, and still echo your error/usage if things aren't right. Once everything is "correct" THEN you start processing, etc. – ivanivan May 15 '17 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.