当我在 bash shell 中运行以下命令时:
if [[ $(readonly | cut -d= -f1 | grep -qo HISTFILE && echo $?) == 0 ]]; then sudo grep -iwo "readonly HISTFILE" /etc/profile /etc/profile.d/*; else echo "HISTFILE not set or not set properly"; fi
上面的命令返回了我想要的结果,即:
/etc/profile.d/os-security.sh:readonly HISTFILE
但是当我将上述命令放入 shell 脚本中时,结果发生了变化,即:
HISTFILE not set or not set properly
这里是完整的外壳:
[ansible@rhel8-hardening-test ~]$ if [[ $(readonly | cut -d= -f1 | grep -qo HISTFILE && echo $?) == 0 ]]; then sudo grep -iwo "readonly HISTFILE" /etc/profile /etc/profile.d/*; else echo "HISTFILE not set or not set properly"; fi
/etc/profile.d/os-security.sh:readonly HISTFILE
[ansible@rhel8-hardening-test ~]$
[ansible@rhel8-hardening-test ~]$
[ansible@rhel8-hardening-test ~]$
[ansible@rhel8-hardening-test ~]$ cat test.sh
#!/bin/bash
if [[ $(readonly | cut -d= -f1 | grep -qo HISTFILE && echo $?) == 0 ]]; then sudo grep -iwo "readonly HISTFILE" /etc/profile /etc/profile.d/*; else echo "HISTFILE not set or not set properly"; fi
[ansible@rhel8-hardening-test ~]$
[ansible@rhel8-hardening-test ~]$
[ansible@rhel8-hardening-test ~]$
[ansible@rhel8-hardening-test ~]$ bash test.sh
HISTFILE not set or not set properly
[ansible@rhel8-hardening-test ~]$
当我使用上述命令的 shell 脚本时,如何才能获得与当前 shell 相同的结果?