处理 bash 脚本的日志和文本文件中的注释
Handling logs of bash script and comments in text file
我正在尝试读取一个几乎没有注释以“#”开头的文本文件,我的 bash 脚本应该读取文本文件中不以“#”开头的行。
我还试图在两个日志中捕获 echo 语句的输出,并将其显示在控制台 window 以供用户理解。
我尝试使用以下查询在控制台中捕获日志和打印
执行 2>&1 1>>$日志文件
为了读取文件的每一行并调用函数,我声明了一个数组并删除以“#”开头的行,我使用了以下查询。
declare -a cmd_array
while read -r -a cmd_array | grep -vE '^(\s*$|#)'
do
"${cmd_array[@]}"
done < "$text_file"
注意:我需要删除以“#”开头的行以及要读取的其余行,并将其放入声明的数组中。
Bash script
***********
#! /bin/bash
Function_1()
{
now=$( date '+%Y%m%d%H%M' )
eval logfile=""_"$now".log
exec 2>&1 1>>$logfile ### Capture echo output in log and printing in console
#exec 3>&1 1>>$logfile 2>&1
echo " "
echo "############################"
echo "Function execution Begins"
echo "############################"
echo "Log file got created with file name as .log"
eval number=
eval path=
echo "number= $number"
ls -lR $path >> temp.txt
if [ $? -eq 0 ]; then
echo " Above query executed."
else
echo "Query execution failed"
fi
echo "############################"
echo "Function execution Ends"
echo "############################"
echo " "
}
text_file=
echo $text_file
declare -a cmd_array ### declaring a array
while read -r -a cmd_array | grep -vE '^(\s*$|#)' ### Read each line in the file with doesnt starts with '#' & keep it in array
do
"${cmd_array[@]}"
done < "$text_file"
Text file
*********
####################################
#Test
#Line2
####################################
Function_1 '125' '' ''
Function_1 '123' '' ''
考虑将 grep 输出通过管道传输到读取中:
declare -a cmd_array ### declaring a array
### Read each line in the file with doesnt starts with '#' & keep it in array
grep -vE '^(\s*$|#)' < "$text_file" | while read -r -a cmd_array
do
"${cmd_array[@]}"
done
我不清楚 output/logging 评论。如果您需要将输出附加到文件,除了 stdout/console),请考虑使用 'tee'(可能是 'tee -a')
我测试了输入文件inputfile
echo a
Function_1 '125' '' ''
# skip me
Function_1 '123' '' ''
echo b
并编写了这个脚本:
declare -a cmd_array ### declaring a array
while read -r -a cmd_array
do
echo "${cmd_array[@]}"
"${cmd_array[@]}"
echo
done < <(grep -vE '^(\s*$|#)' inputfile)
要在日志和控制台中显示输出,请参阅 https://unix.stackexchange.com/a/145654/57293
正如@GordonDavisson 在评论中所建议的那样,您会得到一个类似的结果
source inputfile
忽略注释和空行,调用函数,所以我不确定你为什么想要一个数组。这个命令可以包含在你的主脚本中,你不需要修改输入文件。
采购输入的另一个优点是处理多行输入和 #
字符串:
Function_1 '123' 'this is the second parameter, the third will be on the next line' \
'third parameter for the Function_1 call'
echo "This echo continues
on the next line."
echo "Don't delete # comments in a string"
Function_1 '124' 'Parameter with #, interesting!' ''
我正在尝试读取一个几乎没有注释以“#”开头的文本文件,我的 bash 脚本应该读取文本文件中不以“#”开头的行。 我还试图在两个日志中捕获 echo 语句的输出,并将其显示在控制台 window 以供用户理解。
我尝试使用以下查询在控制台中捕获日志和打印
执行 2>&1 1>>$日志文件
为了读取文件的每一行并调用函数,我声明了一个数组并删除以“#”开头的行,我使用了以下查询。
declare -a cmd_array
while read -r -a cmd_array | grep -vE '^(\s*$|#)'
do
"${cmd_array[@]}"
done < "$text_file"
注意:我需要删除以“#”开头的行以及要读取的其余行,并将其放入声明的数组中。
Bash script
***********
#! /bin/bash
Function_1()
{
now=$( date '+%Y%m%d%H%M' )
eval logfile=""_"$now".log
exec 2>&1 1>>$logfile ### Capture echo output in log and printing in console
#exec 3>&1 1>>$logfile 2>&1
echo " "
echo "############################"
echo "Function execution Begins"
echo "############################"
echo "Log file got created with file name as .log"
eval number=
eval path=
echo "number= $number"
ls -lR $path >> temp.txt
if [ $? -eq 0 ]; then
echo " Above query executed."
else
echo "Query execution failed"
fi
echo "############################"
echo "Function execution Ends"
echo "############################"
echo " "
}
text_file=
echo $text_file
declare -a cmd_array ### declaring a array
while read -r -a cmd_array | grep -vE '^(\s*$|#)' ### Read each line in the file with doesnt starts with '#' & keep it in array
do
"${cmd_array[@]}"
done < "$text_file"
Text file
*********
####################################
#Test
#Line2
####################################
Function_1 '125' '' ''
Function_1 '123' '' ''
考虑将 grep 输出通过管道传输到读取中:
declare -a cmd_array ### declaring a array
### Read each line in the file with doesnt starts with '#' & keep it in array
grep -vE '^(\s*$|#)' < "$text_file" | while read -r -a cmd_array
do
"${cmd_array[@]}"
done
我不清楚 output/logging 评论。如果您需要将输出附加到文件,除了 stdout/console),请考虑使用 'tee'(可能是 'tee -a')
我测试了输入文件inputfile
echo a
Function_1 '125' '' ''
# skip me
Function_1 '123' '' ''
echo b
并编写了这个脚本:
declare -a cmd_array ### declaring a array
while read -r -a cmd_array
do
echo "${cmd_array[@]}"
"${cmd_array[@]}"
echo
done < <(grep -vE '^(\s*$|#)' inputfile)
要在日志和控制台中显示输出,请参阅 https://unix.stackexchange.com/a/145654/57293
正如@GordonDavisson 在评论中所建议的那样,您会得到一个类似的结果
source inputfile
忽略注释和空行,调用函数,所以我不确定你为什么想要一个数组。这个命令可以包含在你的主脚本中,你不需要修改输入文件。
采购输入的另一个优点是处理多行输入和 #
字符串:
Function_1 '123' 'this is the second parameter, the third will be on the next line' \
'third parameter for the Function_1 call'
echo "This echo continues
on the next line."
echo "Don't delete # comments in a string"
Function_1 '124' 'Parameter with #, interesting!' ''