This is weird.. mainly because it worked last night. I have a script that is passed in an expression that is supposed to match file names in an array. I'm not allowed to use find however (it is for an assignment).
For example if the user passes in the argument '*.c', and I have these files to check it against:
~/dir/text1.txt
~/dir/no.c
~/dir/no.sh
I do the comparison in a function like so:
process ()
{
local new_cur=()
for i in "${files[@]}"; do
if [[ "$i" == "$1" ]]; then
new_cur+=("$i")
fi
done
}
I then call this in the bash script like this, where arguments[1] is = to '*.c':
process "${arguments[1]}"
Inside my process function, I checked and the string *.c is being passed in, and the files are being passed in. However it is not passing the if statement within my function.
Note that this also does not work for even simple strings without wildcards.
Edit: I have also looked up common solutions, and for whatever reason none work. Here is what I have tried:
if [ "$i" == "$1" ]
if [ "$1" == "$i" ]
if [ "$i" = "$1" ]
if [ "$1" = "$i" ]
if [[ "$1" = "$i" ]]
if [[ "$i" = "$1" ]]
if [[ "$1" == "$i" ]]
if [[ "$i" == *"$1"* ]]
So far none of the above have worked.
set -xat the beginning?test.cit shows that it is checking[[ test.c == \*\.\c ]]if [ "$i" == "$1" ]now shows it testing for[ test.c = '*.c' ]and passing in just*.cas an argument (no single quotes) shows it as[ test.c = *.c ].. but for whatever reason it still is not showing that it is a match.[[...]]in bash you won't need to put quotes around variables. So[[ $i == $var ]]always works (in contrast to[ "$i" == "$var" ]where quotes are required because otherwise you get errors e.g. if at least one of the variables contain whitespace).