How to use a shell script to check whether a command had been installed in the Linux server All In One
How to use a shell script to check whether a command had been installed in the Linux server All In One
errors ❌
shell script error [: :需要整数表达式
shell script error [: -eq:需要一元表达式
shell script error [: ==:需要一元表达式
#!/usr/bin/env bash
if [[ $(command -v nvm) == nvm ]]; then
echo "❌ nvm not exist, trying to re-install it ... ⏳"
else
echo "nvm had been installed ✅"
fi
#!/usr/bin/env bash
temp=$(command -v nvm)
echo $temp
# if [[ $temp -eq nvm ]]; then
if [ $temp == nvm ]; then
echo "❌ nvm not exist, trying to re-install it ... ⏳"
else
echo "nvm had been installed ✅"
fi
echo finished 🎉
solutions ✅
- 如果 if 语句使用的是
单层
方括号[ ]
条件修饰符, 变量必须加上双引号
- 如果 if 语句使用的是
双层
方括号[[ ]]
条件修饰符, 变量就不需要引号了
#!/usr/bin/env bash
temp=$(command -v nvm)
echo $temp
if [[ $temp == nvm ]]; then
echo "❌ nvm not exist, trying to re-install it ... ⏳"
else
echo "nvm had been installed ✅"
fi
echo finished 🎉
#!/usr/bin/env bash
temp=$(command -v nvm)
echo $temp
if [ "$temp" == nvm ]; then
echo "❌ nvm not exist, trying to re-install it ... ⏳"
else
echo "nvm had been installed ✅"
fi
echo finished 🎉
demos
$ cat /etc/profile
$ ls -lath /etc/profile.d
$ cat /etc/profile
$ ls -lath /etc/profile.d
eric@rpi4b:~/Desktop $ ls -lath /etc/profile.d
总用量 44K
drwxr-xr-x 121 root root 12K 9月 21 23:42 ..
drwxr-xr-x 2 root root 4.0K 9月 21 23:35 .
-rwxr-xr-x 1 root root 81 9月 21 23:35 auto-install-test.sh
-rw-r--r-- 1 root root 324 6月 8 2022 sshpwd.sh
-rw-r--r-- 1 root root 1.4K 2月 18 2021 vte-2.91.sh
-rw-r--r-- 1 root root 966 2月 18 2021 vte.csh
-rw-r--r-- 1 root root 726 8月 12 2020 bash_completion.sh
-rw-r--r-- 1 root root 450 7月 22 2019 wifi-check.sh
-rw-r--r-- 1 root root 95 4月 29 2019 at-dbus-fix.sh
eric@rpi4b:~/Desktop $ cat /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "$(id -u)" -eq 0 ]; then
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games"
fi
export PATH
if [ "${PS1-}" ]; then
if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "$(id -u)" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
# profile test ✅
/home/eric/Desktop/auto-install-profile.sh
eric@rpi4b:~/Desktop $
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
/etc/profile
& /etc/profile.d
boot
vslogin
启动配置文件
加载顺序和优先级
/etc/profile
.bashrc
.zshrc
.profile
corn / corntab
systemctl service
https://www.cnblogs.com/xgqfrms/p/17343088.html
environment variables
vs shell variables
全局
系统
环境变量 / 局部shell
环境变量
https://www.cnblogs.com/xgqfrms/p/17686492.html
refs
https://www.cnblogs.com/xgqfrms/p/15937724.html
https://stackoverflow.com/questions/592620/how-can-i-check-if-a-program-exists-from-a-bash-script
https://www.cnblogs.com/byfboke/articles/9083853.html
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17721396.html
未经授权禁止转载,违者必究!