1

I had written a script for moving/Copying files to particular folder. I need to check the filenames and move to respective folders. I can read the filenamess but, checking the filename contains a string giving the error.

Here my script ::

for eachfile in *; do
  echo "INSIDE"
  echo $eachfile
  if [ [ $eachfile =  "CHPOE05"* ] ]; then
    echo  $eachfile
  else
    echo "invalid"
  fi
done

Below is my error:

Uploading file IN PCS FOLDER
INSIDE
CHPOE0500001.txt
INCOK1IGT1.sh: line 44: [: too many arguments
invalid
INSIDE
CHPOE050i1001.txt
INCOK1IGT1.sh: line 44: [: too many arguments
1
  • Whenever you have a shell script error, a good first step is to cut and paste your code into shellcheck.net and correct the errors (important) and warnings (might be important) that it identifies. If you have trouble understanding its messages, then come here and ask. Commented Mar 7, 2019 at 6:48

1 Answer 1

2

Removing the spaces between [ [ and ] ] seems to work for me.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks.. It works for me also. I had a one more doubt. How i will be adding or condition. Like, if [ [ $eachfile = "CHPOE05"* ] || [ $eachfile = "CHPOE05"* ] ] this way it will work??
If you have a new (unrelated) question, it is better to ask a new question here. Aside from this, I don't quite understand your question here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.