Script was needed
#!/bin/bash # Check if there are two arguments if [ $# -eq 2 ]; then # Check if the input file actually exists. if ! [[ -f "$1" ]]; then echo "The input file $1 does not exist." exit 1 fi else echo "Usage: $0 [inputfile] [outputfile]" exit 1 fi # Run the command on the input file grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" "$1" > "$2"
Change, the script has changed to
grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" $* if [ ! -f "$1" ]; then echo 'Usage: ' echo echo './Scriptname inputfile > outputfile' exit 0 fi
calling a script without parameters does not give erros and sits empty
Usage: ./Scriptname inputfile > outputfile
I have a bit of code
grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" $*
This code pulls out lines containing one word and outputs to a new file, for example
This is a multi word line this the above line is not now once again wrong
The output will be
This now
Code works, users call code with ./scriptname file > newfile
However, I am trying to deploy the code to give users an error message if they incorrectly reference the script.
For the messange error, I am thinking of repeating something like scriptname file_to_process > output_file .
I tried
if [incorrectly invoted unsure what to type] echo $usage exit 1 Usage="usage [inputfile] [>] [outputfile]
However, I’m out of luck. The code works, but does nothing if I only call the script name. Also, if I call the script only with the script name and input file, it will output the results instead of exiting with an error message.
The others I tried
if [ ! -n $1 ]; then echo 'Usage: ' echo echo './Scriptname inputfile > outputfile' exit 0 fi
Given the answers I have received so far, my code is now
#!/bin/bash grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" $* if [ ! -f "$1" ]; then echo 'Usage: ' echo echo './Scriptname inputfile > outputfile' exit 0 fi
When calling a script without an input file, the script does nothing and should be interrupted by ctrl + c, while still trying to get an echo of the invoke message.