gpt4 book ai didi

regex - 脚本在空格上失败,我应该编写 sed 来转义路径或更改脚本以在目录上工作

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

我有一个 samba 目录共享(~1500)示例:101284_2012110634 Sachine 130 AB i HeisMakeBoravia(橄榄色)202373_2012110640 Snowchine 7 AB i 法兰德斯300738_2012110658_Machine VP 7 AB i 重命名 v 500185

我正在编写一个脚本来解析目录并从目录名称中获取一些数据(序列号、名称、注释),并枚举一些文件,特别是列表中目录的子目录(始终相同)。我通过 ls > results.txt 生成了目录列表 我通过 parse.sh results.txt

运行脚本

我完成了脚本的前半部分,它从目录名称中获取我需要的数据(省略了一些行,因为它们不相关)并且它有效。我对脚本的第二部分有问题,它首先查找子目录是否存在,然后查找 file 类型的文件并获取它们的名称和 md5 哈希值。如果在自己没有空格的目录中运行,第二部分将独立工作。

#!/bin/bash
# manually set working directory /sample or /rlisti must combine with parse.sh sample.txt or results.txt
WORKING_DIR='/sample'

# read from input file which is generated from ls dir > results.txt
# usage ./parse.sh results.txt

while IFS= read line || [[ -n $line ]]; do

# some tests which find variables from $line

MACHINE_SN=$(echo "$line" | sed 's/\([0-9][0-9][0-9][0-9][0-9][0-9]\).*/\1/')
BOARD_SN=$(echo "$line" | sed 's/^[0-9][0-9]*[ _]*\([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\).*/\1/')

if echo $line | grep -iqF air
then
AIR='1'
else
AIR='0'
fi

# tests to find and hash particular files in sub dirs
# test if there is directory Actual_Program_and_Database in current line.

if [ -d "$WORKING_DIR"/"$line"/Actual_Program_and_Database ]
then

# if dir exists set SW_DIR to 'Actual_Program_and_Database

SW_DIR='Actual_Program_and_Database'

# count files in dir, there should be 3 or 4, good to know if there are less or more as those are exceptions

NUM_FILES=$(ls "$WORKING_DIR"/"$line"/Actual_Program_and_Database | wc -l)
DB_COUNTER=1 # count Clarion DB files in dir

# test each file - this works on it's own when run in same directory with files
# but fails if lines contain spaces

for FILENAME in "$WORKING_DIR"/"$line"/Actual_Program_and_Database/*
do

# bellow works when script is run from same directory
# FILENAME is just file without it's leading path - so I'm trying to merge it with line

FILENAME_PATH="$WORKING_DIR"/"$line"/Actual_Program_and_Database/"$FILENAME"

# files of type Clarion are DB, there is DB1 and DB2 if there is also DB3 that is exception

if file "$FILENAME" | grep -q Clarion
then

# Create variables with name DB_PATH1 DB_PATH2 etc
# eval works on it's own but breaks if paths contain spaces or ()&

eval 'DB_PATH'$DB_COUNTER="$FILENAME_PATH" # path to filename for use with md5sum
eval 'DB_NAME'$DB_COUNTER=$(basename "$FILENAME_PATH") # name of DBx file
eval 'DB_HASH'$DB_COUNTER=$(md5sum "$FILENAME_PATH" | awk '{ print $1 }') # create hash of DBx
((DB_COUNTER ++)) # increase DB counter

# files of type POSIX are SCRIPTs there should be only one

elif file "$FILENAME" | grep -q POSIX
then
SCRIPT_PATH=$FILENAME_PATH
SCRIPT_NAME=$(basename $FILENAME_PATH)
SCRIPT_HASH=$(md5sum $FILENAME_PATH | awk '{ print $1 }')

# files of type CRLF are LISTs there should be only one

elif file "$FILENAME" | grep -q CRLF
then
LIST_PATH=$FILENAME_PATH
LIST_NAME=$(basename $FILENAME_PATH)
LIST_HASH=$(md5sum $FILENAME_PATH | awk '{ print $1 }')
else
UNKNOWN_FILE='1'
fi
done

# if there isn't directory Actual_Program_and_Database in current line set dir to 0
# there are not enough exceptions to program for them

else
SW_DIR='0'
fi

# Print results (for now - will write SQL statements to populate database once script works)

echo 'serial='$SERIAL
echo 'machine sn = '$MACHINE_SN # Valmar machine SN
echo 'board sn = '$BOARD_SN # electronic board SN
echo 'NUM_FILES = '$NUM_FILES # number of files in directory
echo 'NUM_DBS = '$NUM_DBS # number of files in directory - not working
echo 'DB_COUNTER= '$DB_COUNTER
echo 'SCRIPT_NAME= '$SCRIPT_NAME
echo 'SCRIPT_HASH= '$SCRIPT_HASH
echo 'LIST_NAME= '$LIST_NAME
echo 'LIST_HASH= '$LIST_HASH
echo 'DB_NAME1= '$DB_NAME1
echo 'DB_HASH1= '$DB_HASH1
echo 'DB_NAME2= '$DB_NAME2
echo 'DB_HASH2= '$DB_HASH2
echo 'UNKNOWN_FILE='$UNKNOWN_FILE

# added sleep so I can stop script when it fails to see results

sleep 10

done < "$1"

我是否应该将 sed 添加到开始创建 LINE_ESCAPED ,这将转义行中的所有空格括号并在第二部分中使用它?或者我应该重写脚本以在共享上工作而不是在 results.txt 上工作,这甚至可以工作吗?

最佳答案

我的引用有一些错误,我正在附加路径。我之前使用eval是因为我需要db1= db2=并且我想使用eval来生成变量数字。我从 eval 切换到 array[counter],因为我避免了 eval,而 array 满足了我的需要。不了解数组 - 这是我的第一个更严肃的 bash 脚本。

我还修复了所有大写变量,并使用建议仅测试文件 $filename 一次。这是我现在可以使用的代码。谢谢大家的建议。

if [ -d "$working_dir"/"$line"/Actual_Program_and_Database ]
then
sw_dir='Actual_Program_and_Database'
num_files=$(ls "$working_dir"/"$line"/Actual_Program_and_Database | wc -l) #count files in dir
db_counter=0 # count Clarion DB files in dir
for filename in "$working_dir"/"$line"/Actual_Program_and_Database/*
do
file_type=$(file "$filename")
if [[ $file_type = *Clarion* ]]
then
db_path[$db_counter]=$filename
db_name[$db_counter]=$(basename "$filename")
db_hash[$db_counter]=$(md5sum "$filename" | awk '{ print $1 }')
((db_counter ++))
elif [[ $file_type = *POSIX* ]]
then
script_path=$filename
script_name=$(basename "$filename")
script_hash=$(md5sum "$filename" | awk '{ print $1 }')
elif [[ $file_type = *CRLF* ]]
then
list_path=$filename
list_name=$(basename "$filename")
list_hash=$(md5sum "$filename" | awk '{ print $1 }')
else
unknown_file='1'
fi
done
else
sw_dir='0'
fi

关于regex - 脚本在空格上失败,我应该编写 sed 来转义路径或更改脚本以在目录上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51290287/

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