-3

So I started today taking a look at scripting using vim and I'm just so very lost and was looking for some help in a few areas.

For my first project,I want to process a file as a command line argument, and if a file isn't included when the user executes this script, then a usage message should be displayed, followed by exiting the program.

I have no clue where to even start with that, will I need and if ... then statement, or what?

Will
  • 24,082
  • 14
  • 97
  • 108
JimBob101
  • 173
  • 9
  • Don't torment yourself trying to learn vim and bash as one exercise. Use a simple editor and see the [**Bash Guides**](http://www.tldp.org/guides.html) (beginners and advanced) for starters. – David C. Rankin May 09 '16 at 21:29
  • See [Check if passed argument is file or directory in bash](http://stackoverflow.com/a/4665080/2627975) and distill that down to just the check if file exists part. – Marc K May 09 '16 at 21:31
  • Without any attempt to write a code, you are basically asking for a code. You might as well just ask for a code. The title and content should be "Please write a code for checking if passed parameter is a file". I would suggest, to post a pseudo-code of your understanding then we will help with `bash`-ism. – alvits May 09 '16 at 21:37
  • Please read some simple Bash tutorials before coming to StackExchange. I agree do not try and learn vim at the same time. But what you are asking is very basic shell scripting (accepting arguments) and suggests you have not done much research on your own. – bodangly May 09 '16 at 21:43

2 Answers2

3

Save vim for later and try to learn one thing at a time. A simpler text editor is called nano.

Now, as far as checking for a file as an argument, and showing a usage message otherwise, this is a typical pattern:

PROGNAME="$0"

function show_usage()
{
    echo "Usage: ${PROGNAME} <filename>" >&2
    echo "..." >&2

    exit 1
}

if [[ $# -lt 1 ]]; then
    show_usage
fi

echo "Contents of ${1}:"
cat "$1"

Let's break this down.

PROGNAME="$0"

$0 is the name of the script, as it was called on the command line.

function show_usage()
{
    echo "Usage: ${PROGNAME} <filename>" >&2
    echo "..." >&2

    exit 1
}

This is the function that prints the "usage" message and exits with a failure status code. 0 is success, anything other than 0 is a failure. Note that we redirect our echo to &2--this prints the usage message on Standard Error rather than Standard Output.

if [[ $# -lt 1 ]]; then
    show_usage
fi

$# is the number of arguments passed to the script. If that number is less than 1, print the usage message and exit.

echo "Contents of ${1}:"
cat "$1"

$1 is out filename--the first argument of the script. We can do whatever processing we want to here, with $1 being the filename. Hope this helps!

Will
  • 24,082
  • 14
  • 97
  • 108
1

i think you're asking how to write a bash script that requires a file as a command-line argument, and exits with a usage message if there's a problem with that:

#!/bin/bash

# check if user provided exactly one command-line argument:
if [ $# -ne 1 ]; then
  echo "Usage: `basename "$0"` file"
  exit 1
# now check if the provided argument corresponds to a real file
elif [ ! -f "$1" ]; then
  echo "Error: couldn't find $1."
  exit 1
fi

# do things with the file...
stat "$1"
head "$1"
tail "$1"
grep 'xyz' "$1"
webb
  • 4,180
  • 1
  • 17
  • 26
  • you're welcome :) ps: i just fixed a typo; `if [ -e ... ]` (exists) -> `if [ ! -e ... ]` (doesn't exist). – webb May 09 '16 at 21:35
  • 1
    Couple of things. Wouldn't testing `$#` be relevant? If the user passed a directory name then some of those commands would fail, so `-f` might be a better test. – cdarke May 09 '16 at 21:46