2

I'm new to shell scripting and I'm having some trouble while using the "test" command and the special character * to compare two strings.

I have to write a shell script which, for every element(both files and directories) contained in the directory passed as the first argument, has to write something(for the solving of my problem it is not relevant to know what has to be written down) on the file "summary.out". What's more, there's a string passed as the second argument. Those files/directories beginning with this string must be ignored(nothing has to be written on summary.out).

Here is the code:

#!/bin/bash  
TEMP=NULL
cd "$1"
for i in *
    do 
        if test "$i" != "$2"*;then #Here is where the error comes from
            if test -f "$i";then
                TEMP="$i  -  `head -c 10 "$i"`"
            elif test -d "$i";then
                TEMP="$i  -  `ls -1 "$i" | wc -l`"
            fi
            echo $TEMP >> summary.out
        fi 
done

The error comes from the test which checks whether the current file/directory begins with the string passed as second argument, and it takes place every iteration of the for cycle. It states:"test: too many arguments"

Now, I have performed some tests which showed that the problem has nothing to do with blank spaces inside the $i or $1. The problem is linked to the fact that I use the special character * in the test(if I remove it, everything works fine).

Why can't "test" handle * ? What can I do to fix that?

Matteo
  • 154
  • 12
  • http://www.tldp.org/LDP/abs/html/special-chars.html#ASTERISKREF –  May 05 '15 at 12:53
  • You can use `done > summary.out` instead of `>> summary.out` to avoid opening and closing the file repeatedly in the loop. – Etan Reisner May 05 '15 at 12:58

1 Answers1

2

* gets expanded by the shell.

In bash, you can use [[ ... ]] for conditions instead of test. They support patterns on the right hand side - * is not expanded, as double square brackets are a keyword with higher precedence.

if [[ a == * ]] ; then
    echo Matches
else
    echo Doesn\'t match
fi
choroba
  • 231,213
  • 25
  • 204
  • 289
  • echo is a builtin and `echo *` definitely expands, this answer is very misleading. –  May 05 '15 at 13:02
  • @JID: Sorry, `s/builtin/keyword/`. Fixed. – choroba May 05 '15 at 13:03
  • It's still pretty misleading `*` is not expanded because it is an argument to test(also FWIW test is a bultin) . It is expanded all the time with anything unless something else with higher precedence is interpereted first(such as `""` `''` or `[[]]`) and then prevents it from doing so.As an argument to `if`,another keyword, it will expand just the same as with anything else. –  May 05 '15 at 13:17
  • @JID: keywords can decide what to do with `*`, but commands can't. – choroba May 05 '15 at 13:22
  • yes because they are parsed first as i said, but you don't say that in your answer, you have made blanket statements that are misleading. I never said anything was wrong, just that your answer is misleading. –  May 05 '15 at 13:26
  • @JID: I tried to reword the answer. – choroba May 05 '15 at 13:27
  • Thankyou, that is clearer :) –  May 05 '15 at 13:28