2012-04-29 29 views
0

我正在尝试将适用于Linux的脚本安装到我的WD世界版驱动器上。Ash MATCH运算符(=〜)

该脚本是为Bash(debian)编写的,但我的WD只运行busybox(with ash)。尽管如此,仅仅使用Google,我已经获得了大部分功能。只有一个操作员,我还没有找到对应的操作员,=〜操作员

如何将=〜操作符的功能从旧脚本移植到灰?

脚本:

#! /bin/bash 
# posttorrent.sh by Killemov 
{ 
    # Log file, file where we tell what events have been processed. 
    LOG_FILE=/var/log/posttorrent.log 
    # Username for transmission remote. 
    TR_USERNAME="username" 
    # Password for transmission remote. 
    TR_PASSWORD="password" 
    # Get current time. 
    NOW=$(date +%Y-%m-%d\ %H:%M:%S) 
    # Source directory, should not be changed. 
    SRC_DIR="${TR_TORRENT_DIR}/${TR_TORRENT_NAME}" 
    # Directory to store the un-compressed files in.. 
    DEST_DIR="${TR_TORRENT_DIR}/${TR_TORRENT_NAME}/" 
    # This parameter string could be passed from Transmission in the future. 
    TR_TORRENT_PARAMETER="EXTRACT SLEEP1h" 

echo "text" 
    if [ -e "$SRC_DIR/keep" ]; then 
    TR_TORRENT_PARAMETER="$TR_TORRENT_PARAMETER KEEP" 
    fi 

    if [ -e "$SRC_DIR/exit" ]; then 
    TR_TORRENT_PARAMETER="EXIT" 
    fi 

    # Actual processing starts here. 
    if [[ "$TR_TORRENT_PARAMETER" =~ "EXIT" ]]; then 
    echo $NOW "Exiting $TR_TORRENT_NAME" >> $LOG_FILE 
    exit 0 
    fi 
echo "text2" 
    if [[ "$TR_TORRENT_PARAMETER" =~ "EXTRACT" ]]; then 
    cd $TR_TORRENT_DIR 
    if [ -d "$SRC_DIR" ]; then 
     IFS=$'\n' 
     unset RAR_FILES i 
     for RAR_FILE in $(find "$SRC_DIR" -iname "*.rar"); do 
     if [[ $RAR_FILE =~ .*part.*.rar ]]; then 
      if [[ $RAR_FILE =~ .*part0*1.rar ]]; then 
      RAR_FILES[i++]=$RAR_FILE 
      fi 
     else 
      RAR_FILES[i++]=$RAR_FILE 
     fi 
     done 
     unset IFS 

     if [ ${#RAR_FILES} -gt 0 ]; then 
     for RAR_FILE in "$(eval \$$RAR_FILES[@])"; do 
      unrar x -inul "$RAR_FILE" "$DEST_DIR" 
      if [ $? -gt 0 ]; then 
      echo $NOW "Error unrarring $TR_TORRENT_NAME" >> $LOG_FILE 
      transmission-remote -n $TR_USERNAME:$TR_PASSWORD -t$TR_TORRENT_ID --verify --start 
      exit 0 
      fi 
     done 
     if [[ ! "$TR_TORRENT_PARAMETER" =~ "KEEP" ]]; then 
      SLEEP=$(expr match "$TR_TORRENT_PARAMETER" '.*SLEEP\([0-9a-zA-Z]*\)') 
      if [ ${#SLEEP} -gt 0 ]; then 
      sleep $SLEEP 
      fi 
      transmission-remote -n $TR_USERNAME:$TR_PASSWORD -t$TR_TORRENT_ID --remove-and-delete 
     fi 
     echo $NOW "Unrarred $TR_TORRENT_NAME" >> $LOG_FILE 
     fi 
    fi 
    fi 
} & 

(我遇到了一些麻烦间接引用,我希望自己固定的正确)

回答

0

好为$VARIABLE =~ PATERN,你应该能够使用:

echo "$VARIABLE" | grep -E PATTERN 

但是我想你的算术表达式i++也会有点麻烦 - 如果它被实现了,如果没有实现,则需要使用i=$(($i + 1))语法,然后使用i=$(expr $i + 1)语法。

我相信你是原因IFS = $“\ n”是拆就换行的查找,但你可能与发出寻找到一个临时文件,然后做一个while read line; do ... done <$tmpfile更好,

另外,我不确定busybox ash的所有版本是否支持数组,所以你可能在那里也有问题。

+0

是的。我试着运行脚本,虽然它看起来很好,但10分钟后,当我回来时,我在数组[i ++]行之后发现了“未找到”垃圾邮件。因为我不是一个bash/ash人,所以我觉得我最好选择别的东西,不幸的是。 –

+0

'grep -Eq'稍好,因为它不打印任何东西。 –