Spyros_av
Spyros_av

Reputation: 893

Shell Script :How to pass script's command-line arguments through to commands that it invokes?

So far "ls" works fine i take all files in the directory. But now i want when i execute ./myscript ls -l /somedir to take the same results as i take when i type ls -l /somedir at the terminal.

Is there any way to make it? This is my code so far..

#!/bin/sh
clear

echo ""
read  -p "type something :  " file
    echo""
    IFS=:
    for dir in $PATH ; do  

        if [ -x "$dir/$file" ]
        then

        echo ""
          exec "$dir/$file" 

        fi
        done

Upvotes: 0

Views: 105

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295291

As I understand it (which involves a great deal of guesswork, as the question is not clearly asked), your problem is that command-line arguments aren't passed through.

Use "$@" to access them:

#!/bin/bash
prog=$1; shift
IFS=: read -r -a paths <<<"$PATH"
for path in "${paths[@]}"; do
  [[ -x $path/$prog ]] && "$path/$prog" "$@"
done

Then, running yourscript ls -l /foo will actually pass the -l and the /foo through to any instance of ls they create.

Upvotes: 2

Related Questions