0

Hello I am trying to create parameters for my shell script but I am having trouble.

lets say for example the file is called test.

When I call ./test -parameter1 input_file.txt I get an error saying 'no such file or directory'.

Here is an example of my code.

#!/bin/sh

if [ "$1" == "parameter" ]
then
    while read line
    do
        #echo "$line"
    done <$1
else
    echo "Not working"
fi

My over all goal of this is to read in a file of numbers line by line which I have working, then to calculate the average values of the rows or by columns. Which is why I am trying to create parameters so the user will have to specify ./test -rows input_file.txt or ./test -columns input_file.txt

2
  • It would be clearer if you named your script [ instead of test. (That is an attempt at subtle humor, what I really mean is that you should not name your script test, since that is commonly a shell builtin and keeping that name will only lead to confusion.) Commented Mar 31, 2015 at 16:41
  • I have my script called 'stats', was just using 'test' as a generic example. Thanks for the input though, I just started learning and practicing making Linux shell scripts. Commented Mar 31, 2015 at 17:10

1 Answer 1

1

You are using the string -parameter as the input file name. Perhaps you want:

#!/bin/sh

if [ "$1" = "-parameter" ]
then
    while read line
    do
        #echo "$line"
    done <$2   # Use $2 instead of $1 here.  Or use shift
else
    echo "Not working" >&2
fi
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.