0

I have a script that uses grep to extract data from a file. It works but I am now trying to modify the script for pipeline use. If I use the commands: cat file | myScript.sh there is an error because the data reaches myScript.sh as it was collected by an echo command (grep: LINE: No such file or directory).

Is there a way to pass 'file' as an argument to myScript.sh so that grep can work as it was originally meant?

2
Contributors
1
Reply
17
Views
3 Months
Discussion Span
Last Post by JasonHippy
2

Depending on the actual content of myScript.sh, you might be able to use input redirection to use the file as input to the script:
./myScript.sh < /path/to/file

Or if file contains a list of file-names to search - you might be able to use xargs:
cat file | xargs ./myScript.sh
But again, xargs usage would depend on what myScript.sh actually does. We don't really know anything about your script, what parameters it is set up to take etc etc.

Or you could just set up your script to explicitly take the path to a file as a parameter:
e.g.

#!/usr/bin/env bash

# if number of parameters to script is 1 AND the parameter is the path to a file
if [ $# -eq 1 ] && [ -f $1 ] ; then
    grep -iHn --color "regex" $1         # Search the file
else
    echo "ERROR - Script takes 1 parameter which must be the path to an existing file"
    exit 1                                    # Exit with error status
fi

And then you would run your script like this:
./myScript.sh /path/to/file

Obviously the above example was completley contrived. I don't know what your actual script does. But my example should give you an idea on how to modify your script to take a file-name as a parameter, if that is what you want to do.....

Edited by JasonHippy

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.