[英]Commands Working In Terminal, But Not In Bash Script
I'm sysadmin for couple of webservers deployed with cpanel hosting panel. 我是使用cpanel托管面板部署的几台Web服务器的系统管理员。 I'm trying to finish up with a backup script. 我正在尝试完成一个备份脚本。 There are two commands bundled with Cpanel, that will be used in this script. Cpanel捆绑了两个命令,这些命令将在此脚本中使用。 These commands are; 这些命令是:
1. whmapi1 modifyacct user=USERNAME BACKUP=[01]
This Command has booleans to set, what it does is either enable or disable backup for a specific user. 此命令具有要设置的布尔值,它的作用是为特定用户启用或禁用备份。
2. /usr/local/cpanel/bin/backup --force
Once backup is enabled for a user/users, then this command starts the backup process on the server. 为一个或多个用户启用备份后,此命令将在服务器上启动备份过程。
So here is my script logic & the script. 这是我的脚本逻辑和脚本。
#!/bin/bash
L=($( comm -23 <(du -h --max-depth=1 /home 2>/dev/null | grep G | awk -F"/" '{print $NF}' | sort | egrep -vw '(home|virtfs)') <(ls -al /var/cpanel/suspended/ | grep -v 'lock' | sort) ))
Above Array contains all the account whose home directories have exceeded 1GB limit. Array上方包含其主目录超过1GB限制的所有帐户。
S=($(comm -23 <(du -h --max-depth=1 /home 2>/dev/null | egrep -v '(!G|.cp|cP|clamav)' | awk -F"/" '{print $NF}' | sort | egrep -vw '(home|virtfs)') <(ls -al /var/cpanel/suspended/ | grep -v 'lock' | sort) ))
Above Array contains all the account whose home directories are less than 1GB limit. Array上方包含其主目录小于1GB限制的所有帐户。
whmapi1 modifyacct user=${L[@]} BACKUP=0 && whmapi1 modifyacct user=${S[@]} BACKUP=0
Above command disables backup for all users for start, to start from scracth. 以上命令禁用所有用户的备份,以便从scracth开始。
whmapi1 modifyacct user=${S[@]} BACKUP=1
T Ť
his command enables backup for all accounts whose home dirs are less than 1 GB 他的命令为所有家庭目录小于1 GB的帐户启用备份
/usr/local/cpanel/bin/backup --force
This command starts backup process for all enabled users. 该命令为所有启用的用户启动备份过程。
The logic is, that I want to create backup of small accounts first, and then when it's finished, I'll run it for larger accounts. 逻辑是,我要先创建小帐户的备份,然后在完成后为大型帐户运行它。
PROBLEM : all commands execute successfully when run directly in terminal, but it doesn't when run via a script. 问题 :直接在终端中运行时,所有命令都成功执行,但通过脚本运行时,则不能成功执行。 Problem occurs at account enabling & disabling. 在启用和禁用帐户时出现问题。 It either disables all or enables all, and not the partial accounts, as intended by the logic of the script. 根据脚本逻辑的意图,它要么禁用所有帐户,要么启用所有帐户,而不是部分帐户。
Can anyone point out, where & what I'm missing? 谁能指出我想念的地方和内容? Thanks in advance !! 提前致谢 !!
${l[@]}
exands to user1 user2 user3 ...
, so user=${L[@]}
expands to user=user1 user2 user3 ...
, if you want to fun foreach user, you need to loop over users. ${l[@]}
扩展为user1 user2 user3 ...
,因此user=${L[@]}
扩展为user=user1 user2 user3 ...
,如果要对每个用户进行娱乐,则需要遍历用户。
du_buff=$(du -h --max-depth=1 /home 2>/dev/null)
lock_buff=$(ls -al /var/cpanel/suspended/ | grep -v 'lock' | sort)
L=($(comm -23 <(echo "$du_buff" | grep G | awk -F"/" '{print $NF}' | sort | egrep -vw '(home|virtfs)') <(echo "$lock_buff") ))
S=($(comm -23 <(echo "$du_buff" | egrep -v '(!G|.cp|cP|clamav)' | awk -F"/" '{print $NF}' | sort | egrep -vw '(home|virtfs)') <(echo "$lock_buff") ))
# for every user in L and S
for user in "${L[@]}" "${S[@]}"; do
whmapi1 modifyacct user=$user BACKUP=0
done
# for every user in S
for user in "${S[@]}"; do
whmapi1 modifyacct user=$user BACKUP=1
done
/usr/local/cpanel/bin/backup --force
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.