0

I'm writing a bash script to organise a .txt file, of which is loaded with the bash script using the command line parameter:

bash ./myScript.sh textfile.txt

I have an if statement (below, non-functional) that's supposed to detect if the .txt file exists in the same directory. If it does, it confirms it with the user and the script continues. If it doesn't exist, the script is supposed to continually check if the user's input is an existing file in the working directory before continuing.

Here's what I have so far:

    #CS101 Assignment BASH script
CARFILE=$1
wc $CARFILE
if [ -f $CARFILE ]
then 
    echo "$CARFILE exists, please continue"
else
    echo "This file does not exist, please enter the new filename and press [ENTER]"
read CARFILE
echo "We have detected that you're using $CARFILE as your cars file, please continue."
fi

It simply outputs: exists, please continue if you don't run it with a .txt (ie bash jag32.sh instead of bash jag32.sh textfile.txt).

Can anyone help out please?

Thanks.

1
  • Is there a better way of detecting if the user enters a file along with the bash script instead of using $CARFILE? Commented Aug 13, 2015 at 22:47

1 Answer 1

2

There are two problems here. First, you're calling wc $CARFILE before you check if $CARFILE exists. If you specify no paramaters (bash jag32.sh), then this becomes simply wc, which will wait forever for input on stdin.

Before going any further, bash -x is your friend: this will trace the execution of your script, showing you exactly what commands the script is running. This will often help illuminate problems:

bash -x jag32.sh

The problem with your test:

if [ -f $CARFILE ]

Is that if $CARFILE is empty, it becomes simply:

if [ -f ]

Which evaluates to true. What!? That's because the file-existence test takes two parameters (-f FILE), and when $CARFILE is empty, you only have one, so it's evaluating as a simple is-this-string-empty? test.

And this is why you always quote variables:

if [ -f "$CARFILE" ]

If $CARFILE is empty, this becomes:

if [ -f "" ]

Which will evaluate to false.

In addition to checking for the existence of the file, you could also first check if $1 has any value, or if your script has been passed any arguments. The bash(1) and test(1) man pages have details that should point you in the right direction.

Sign up to request clarification or add additional context in comments.

2 Comments

That's amazingly simple and worked perfectly, thanks a lot. I imagine using a while loop to wait for the user to enter a .txt file if $CARLIST comes up false would be correct?
Changed the if statement to a while loop and it's all working perfectly, thanks again for your help @larsks, I'll mark your answer as correct :)

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.