Linux shell脚本中如何读取跟shell脚本同一目录下的配置文件
ymc4
10年前
配置文件如下
##########BACKUP MYSQL CONFIG####### #database name db_name=test db_user=root db_pass=123456 #####backup day config begin ##### day_backupdir=/home/backup/db/day day_backup_fix=day day_interval=1 #####backup day config end ####### #####backup week config begin ##### week_backupdir=/home/backup/db/week week_backup_fix=week week_interval=2 #####backup week config end ####### #####backup month config begin ##### month_backupdir=/home/backup/db/month month_backup_fix=month month_interval=3 #####backup month config end #######
名称为: db_config
shell脚本如下:
#!/bin/bash # Name:bak_ftp_day.sh # This is a ShellScript For Auto File Backup and Delete old Backup # #DIR变量为当前shell脚本的目录 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #使用source命令导入平级目录下的配置文件,引入以后就可以直接用配置中的变量名来获取文件中的值了 source $DIR"/"db_config time=_` date +%Y_%m_%d_%H_%M_%S `_ echo "------bakup---<<<--`date +%Y-%m-%d-%H-%M-%S`----begin--->>>---"; echo $day_backupdir/$db_name$time$day_backup_fix.sql.gz"---is --begin---"; mysqldump -u $db_user -p$db_pass $db_name | gzip > $day_backupdir/$db_name$time$day_backup_fix.sql.gz echo $day_backupdir/$db_name$time$day_backup_fix.sql.gz"---is --finsh---"; find $day_backupdir -name $db_name"*.sql.gz" -type f -mmin +$day_interval -exec rm -rf {} \; > /dev/null 2>&1 echo "------bakup----<<<---`date +%Y-%m-%d-%H-%M-%S`----finsh--->>>---"; echo "";