如何将 bash 中的查询字符串解析为 if then 语句
How to parse query string in bash into if then statement
如何使用 bash 扫描文本文件中的特定字符串,然后使用 if then 语句根据字符串执行特定命令?
我正在尝试使用 rsync 从 HTML 表单输出的查询字符串中备份一些树莓派。我只有很少的 bash 经验,我已经研究和研究这段代码好几天了,我很想得到一些建议。
QUERY_STRING 将包含类似于 "Backup_To_Comp=tasting-side_backup&subbtn=Submit" 的内容,其中 "Tasting-side_backup" 被替换为所选的其他径向按钮标签。
#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "$QUERY_STRING" > /var/www/cgi-bin/scan.txt
BackupToCompFrom=`echo "$QUERY_STRING" | sed -n 's/^.*Backup_To_Comp=\([^&]*\).*$//p' | sed "s/%20/ /g"`
echo "<html><head><title>What You Said</title></head>"
echo "<body>Here's what you said:"
echo "You entered $BackupToCompFrom in from field."
sleep 1
file="/var/www/cgi-bin/scan.txt"
##echo "$QUERY_STRING"
while IFS='' read -r line || [[ -n "$line" ]];
do
if [[ $line = "tasting-side_backup" ]]; then
echo "GotIt."
rsync pi@192.168.1.1:/home/pi/screenly_assets /home/pi/Downloads
elif [[ $line = "~tasting-main"* ]]; then
print "Tasting Main"
elif [[ $line = "~lodge"* ]]; then
print "Lodge"
elif [[ $line = "~barn"* ]]; then
print "Barn"
else
print "Please select a pi to copy from!"
fi
done
How can I use bash to scan for a specific string of characters in a text file and then use an if then statement to execute a specific command depending on the string?
您可以在 if 语句中使用命令。现在该命令的退出代码用于确定真假。对于文件查询的简单搜索,您可以使用 grep。
if grep -q "$QUERY_STRING" file; then
-q 用于防止 grep 的任何输出在标准输入中结束。
如何使用 bash 扫描文本文件中的特定字符串,然后使用 if then 语句根据字符串执行特定命令? 我正在尝试使用 rsync 从 HTML 表单输出的查询字符串中备份一些树莓派。我只有很少的 bash 经验,我已经研究和研究这段代码好几天了,我很想得到一些建议。
QUERY_STRING 将包含类似于 "Backup_To_Comp=tasting-side_backup&subbtn=Submit" 的内容,其中 "Tasting-side_backup" 被替换为所选的其他径向按钮标签。
#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "$QUERY_STRING" > /var/www/cgi-bin/scan.txt
BackupToCompFrom=`echo "$QUERY_STRING" | sed -n 's/^.*Backup_To_Comp=\([^&]*\).*$//p' | sed "s/%20/ /g"`
echo "<html><head><title>What You Said</title></head>"
echo "<body>Here's what you said:"
echo "You entered $BackupToCompFrom in from field."
sleep 1
file="/var/www/cgi-bin/scan.txt"
##echo "$QUERY_STRING"
while IFS='' read -r line || [[ -n "$line" ]];
do
if [[ $line = "tasting-side_backup" ]]; then
echo "GotIt."
rsync pi@192.168.1.1:/home/pi/screenly_assets /home/pi/Downloads
elif [[ $line = "~tasting-main"* ]]; then
print "Tasting Main"
elif [[ $line = "~lodge"* ]]; then
print "Lodge"
elif [[ $line = "~barn"* ]]; then
print "Barn"
else
print "Please select a pi to copy from!"
fi
done
How can I use bash to scan for a specific string of characters in a text file and then use an if then statement to execute a specific command depending on the string?
您可以在 if 语句中使用命令。现在该命令的退出代码用于确定真假。对于文件查询的简单搜索,您可以使用 grep。
if grep -q "$QUERY_STRING" file; then
-q 用于防止 grep 的任何输出在标准输入中结束。