I'm new to bash scripting and I'm writing a script to automatically make a newly created .sh
file executable. As such, I would want to check if .sh
is preceded by the only argument provided in order to ensure a valid filename.
However, I'm getting a [: too many arguments
error when I run ./createExecutableScript.sh asd.sh
.
Initially, I thought it was because $1
was not wrapped in "
that caused the error, and therefore added the "
s in.
I also referred to this question
(Linux Shell Script - String Comparison with wildcards) and added the *
s outside of the "
s.
#!/bin/bash
if [ "$#" -eq 1 ] && [ "$1" == *".sh" ]
then
echo "Creating file with name: $1"
else
echo "Invalid or missing filename!"
fi