简体   繁体   English

命令在终端中运行,但不在Bash脚本中运行

[英]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

Arrays 数组

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.

相关问题 字符串匹配在bash脚本中不起作用 - String matching not working in bash script 我可以在bash脚本中使用命令和文件名向grep发送字符串吗? - Can I send a string to grep with commands and file names in a bash script? 使用文件输入运行并行命令的Bash脚本 - Bash script for running parallel commands using input from a file 在 Bash 脚本中尝试将局部变量传递给 SSH 然后执行其他命令 - In Bash script trying to pass local variable to SSH and then execute the other commands bash 脚本从数组中的列表中检查已安装的字体和 for 循环以执行命令 - bash script checking for installed fonts from list in an array and for loop to do commands 替换用户mid-Bash-script并继续运行命令(Mac OSX) - Substitute user mid-Bash-script & continue running commands (Mac OSX) 如何遍历通过终端传递给Ruby脚本的bash数组变量的元素? - How do I loop through elements of a bash array variable passed to a Ruby script via terminal? 有没有一种简单的方法可以使用小脚本自动读取文本文件并使用 bash 脚本和 awk 或其他 unix 命令进行绘图? - Is there an easy way to use a small script to automate reading of a text file and plotting using bash script with awk or other unix commands? 为什么我的bash shell脚本在v 3.2.5上运行而在4.1.2上失败? - Why is my bash shell script working on v 3.2.5 and failing on 4.1.2? Bash 正则表达式和将命令存储到变量中 - Bash RegEx and Storing Commands into a Variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM