gpt4 book ai didi

linux - Ash MATCH 运算符 (=~)

转载 作者:太空宇宙 更新时间:2023-11-04 04:24:24 30 4
gpt4 key购买 nike

我正在尝试将 Linux 脚本安装到我的 WD 世界版驱动器上。

该脚本是为 Bash (debian) 编写的,但我的 WD 只运行 busybox(带有 ash)。尽管如此,我还是通过使用 Google 获得了其中的大部分功能。只有一个运算符我还没有找到对应的,即 =~ 运算符

如何将 =~ 运算符的功能从旧脚本移植到 ash?

脚本:

#! /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
} &

(我在间接引用方面遇到了一些麻烦,我希望我能正确修复该问题)

最佳答案

好吧$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 都支持数组,因此您可能也会遇到问题。

关于linux - Ash MATCH 运算符 (=~),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10373529/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com