2

For a final project, I'm trying to create a shell script that will allow for easy user creation. The requirements include that I read information from a file and create a user based off of that. This is a snippet of code I'm having trouble with.

echo -n "Please provide a path to the file containing these parameters: "
read inputPath 

fileInfo = $(grep $inputPath) 

if [ -n "$fileInfo" ]; then 
    IFS="," read firstName lastName phoneNo emailAd < "$fileInfo" 
else
    echo "Cannot read, file is empty." >&2 
    exit 1
fi

After read inputPath It will not stop accepting input and continue onto the next line of code. I'm totally stumped.

Thanks for taking a look.

1
  • 4
    Please take a look: shellcheck.net Commented Jul 28, 2016 at 5:48

1 Answer 1

2

This won't work at all as an assignment statement:

fileInfo = $(grep $inputPath) 

The shell does no allow spaces around = in an assignment. Even with we fix that problem, we will still have another:

fileInfo=$(grep $inputPath) 

If inputPath is a path with no spaces in it, then grep will look for that string in stdin and it will continue looking until you press ctrl-D to close stdin.

You demonstrate this by trying it at the command prompt:

$ inputPath=/usr/bin/python
$ grep $inputPath

The shell will wait for you to provide input.

Most likely, you wanted to search for something in the file whose name is $inputPath:

fileInfo=$(grep something "$inputPath") 

Note that we have quoted inputPath to prevent word splitting and pathname expansion.

More on the command with spaces

Let's look at this again:

fileInfo = $(grep $inputPath) 

If the shell sees this command, it will try to run the command fileInfo with first argument = and with the remaining arguments being the result of word splitting and pathname expansion on whatever $(grep $inputPath) produces. But, to do this, as Jonathan Leffler points out in the comments, the shell must first run grep $inputPath. As long as $inputPath does not contain whitespace, then grep will read stdin looking for text that matches the value of $inputPath. Unless ctrl-D is typed, the shell will appear to hang here.

In other words, even though the version of the command with the extra spaces has multiple problems, the first problem to be noticed will be that grep is waiting for stdin.

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

3 Comments

The other issue is with the incomplete grep expression. I see grep and i presume the regex is $inputPath, but then applied to what? (edit fixed it)
Note that the spaced out expression will 'run' but the grep will be run first, so the fact that there probably isn't a command called fileInfo isn't noticed — unless you type control-D to indicate EOF. The $(grep $inputPath) is the primary cause of the trouble. And basic How to debug a shell script technique would show that it was the grep and not the read causing trouble.
@JonathanLeffler That is fascinating. Answer updated with a discussion of that.

Your Answer

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