Monday, March 2, 2015

Synology default shell does not support regex

I was trying to write a script today that would parse a file and check if a line matches a regex. It would fail for unknown reasons, until I found out that the default shell within a Synology (ash), does not support this. Installing bash with ipkg solves this.

Script (not working in ash):
#!/bin/sh

FILENAME="status.txt"
LINENUM=0
REGEX="^(CLIENT_LIST)(.+)"

while read SINGLELINE
do
 LINENUM=$((LINENUM+1))

  if [[ $SINGLELINE =~ $REGEX ]]; then
  NUM_CLIENT=$((NUM_CLIENT+1))
  echo "Match CLIENT_LIST: $LINENUM"
 else
  echo "No match: $LINENUM"
 fi

done < "$FILENAME"

In my terminal:
$ ./status.sh
sh: 2.3.6: unknown operand

Script (working under bash):
#!/opt/bin/bash

FILENAME="status.txt"
LINENUM=0
REGEX="^(CLIENT_LIST)(.+)"

while read SINGLELINE
do
 LINENUM=$((LINENUM+1))

 if [[ $SINGLELINE =~ $REGEX ]]; then
  NUM_CLIENT=$((NUM_CLIENT+1))
  echo "Match CLIENT_LIST: $LINENUM"
 else
  echo "No match: $LINENUM"
 fi
done < "$FILENAME"

In my terminal:
$ ./status.sh
./status.sh
No match: 1
No match: 2
No match: 3
Match CLIENT_LIST: 4
No match: 5
No match: 6
No match: 7

Tip from: http://forum.synology.com/enu/viewtopic.php?f=27&t=77899

No comments:

Post a Comment