Bash脚本无法读取整个文件 [英] Bash script fails to read entire file
本文介绍了Bash脚本无法读取整个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个bash脚本,用于备份一组文件夹/文件.该脚本循环遍历一个预先配置的文件,以确定如何复制该文件夹/文件.
I have a bash script for backing up a set of folders/files. The script loops through a pre-configured file to determine how to copy the folder/file.
我面临的问题是脚本在处理了第一个文件"项(第二列值)之后退出了while循环.
The problem I am facing is the script exits the while loop after processing the first "file" entry (2nd column value).
这是脚本.
#!/bin/bash
sudo crontab -l > /home/ipaccess/crontab.txt
DATE=`date +%m-%d-%Y`
TARGETDIR="/exports/backup/Web-Server/$DATE/"
BACKUPLIST="/home/ipaccess/backup/backuplist"
ssh 172.29.32.246 mkdir -p $TARGETDIR
scp -r /home/ipaccess/crontab.txt 172.29.32.246:$TARGETDIR
cat $BACKUPLIST
while read dir type args
do
echo "Processing [$dir $type $args]"
echo "Directory: $dir"
echo "Element type: $type"
if [ $type = "folder" ] ; then
echo "Source: $args"
echo "Transfering $args to 172.29.32.246:$TARGETDIR$dir"
scp -r $args 172.29.32.246:$TARGETDIR
fi
if [ $type = "file" ] ; then
# Create folders first
echo "Creating $TARGETDIR$dir"
ssh 172.29.32.246 mkdir -p $TARGETDIR$dir
scp $args 172.29.32.246:$TARGETDIR$dir
fi
echo "Finished processing $dir $type $args"
echo "Next entry.."
done<$BACKUPLIST
echo "Backup complete."
这是预先配置的文件(备份列表)
Here is the pre-configured file (backuplist)
html/GS folder /var/www/html/GS/css
html/GS folder /var/www/html/GS/images
html/GS file /var/www/html/GS/*.html
html/GS file /var/www/html/GS/*.inc
html/GS file /var/www/html/GS/*.png
html/GS file /var/www/html/GS/*.mysql
html/GS file /var/www/html/GS/ac-db
html/HC file /var/www/html/HC/*.php
html/HC file /var/www/html/HC/*.html
html/HC file /var/www/html/HC/images
html/HC file /var/www/html/HC/PM/*.php
html/HC/PM file /var/www/html/HC/PM/jsapi
html/HourlyKpi file /var/www/html/HourlyKpi/*.php
html/KPI folder /var/www/html/KPI
html/KpiQuery folder /var/www/html/KpiQuery
html/wordpress/ folder /var/www/html/wordpress
usr/local/apache2/conf file /usr/local/apache2/conf/httpd.conf
cgi-bin folder /var/www/cgi-bin
html/GS file /var/www/html/GS/*.php
这是脚本的输出
Processing [html/GS file /var/www/html/GS/*.html]
+ echo 'Directory: html/GS'
Directory: html/GS
+ echo 'Element type: file'
Element type: file
+ '[' file = folder ']'
+ '[' file = file ']'
+ echo 'Creating /exports/backup/Web-Server/04-18-2013/html/GS'
Creating /exports/backup/Web-Server/04-18-2013/html/GS
+ ssh 172.29.32.246 mkdir -p /exports/backup/Web-Server/04-18-2013/html/GS
+ scp /var/www/html/GS/gs.html /var/www/html/GS/login_header.html /var/www/html/GS/nologin_header.html /var/www/html/GS/overlaynal.html /var/www/html/GS/tab-test.html 172.29.32.246:/exports/backup/Web-Server/04-18-2013/html/GS
gs.html 100% 3204 3.1KB/s 00:0
login_header.html 100% 418 0.4KB/s 00:0
nologin_header.html 100% 464 0.5KB/s 00:0
overlay-external.html 100% 2308 2.3KB/s 00:0
tab-test.html 100% 1573 1.5KB/s 00:0
+ echo 'Finished processing html/GS file /var/www/html/GS/*.html'
Finished processing html/GS file /var/www/html/GS/*.html
+ echo 'Next entry..'
Next entry..
+ read dir type args
+ echo 'Backup complete.'
Backup complete.
推荐答案
循环中对 ssh
的调用从标准输入中读取,即使您提供的命令未使用标准输入.使用 -n
选项:
The call to ssh
in your loop reads from standard input, even though the command you give it doesn't use the standard input. Use the -n
option:
ssh -n 172.29.32.246 mkdir -p $TARGETDIR$dir
从/dev/null
重定向标准输入.
这篇关于Bash脚本无法读取整个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文