0

I want to create a script that receives a list of parameters in the following structure:

  • The first parameter: file or directory
  • The other parameters: the names of the commands in the system, for example date, pwd, tail, head, ls.

The script will run all the commands (starting from the second parameter) on the file/directory received as the first parameter.

I tried this script but I can't use the commands options like -l -i -n so how can i do this?

#!/bin/bash

name=$1
shift

for cmd in "$@" 
do
    if [ -e $name ]
    then
        echo Executing:$cmd
        ($cmd $name)2>/dev/null
        if [ $? -ne 0 ]
        then
            echo error
        fi
    else
        exit 7
    fi
done

1 Answer 1

0

getopts or getopt is the answer.

getopts works on short keys, -i for example and easier to work with. It is also builtin to bash and always available.

getopt is an external tool, it can work with long parameters like --input-file, and a little harder to use. This is an external tool and in theory can be absent, but...

Read here for easy tutorial: https://dustymabe.com/2013/05/17/easy-getopt-for-a-bash-script/

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.