How to use a shell script to check whether a command had been installed in the Linux server All In One
1.Linux shell command ln All In One2.Linux shell command cut All In One3.Linux shell standard input bugs All In One4.Linux shell command base64 All In One5.How to use the shell command to get the version of Linux Distributions All In One6.Linux shell command make & Makefile All In One7.Linux shell command strings All In One8.sudo & su & Rust All In One9.Linux shell command make All In One10.Linux shell script shebang env All In One11.Linux shell script switch...case All In One12.Linux shell command ls sort by date All In One13.Linux shell script read file line by line All In One14.Linux shell regular expression All In One15.How to fix IP filter regular expressions written using grep command in Linux shell script All In One16.Linux shell script programming All In One17.Linux shell script redirection All In One18.How to use Linux shell command filter the IP address All In One19.how to create one interactive mode command line configuration tool with shell language on Linux All In One20.Linux shell script get date and time All In One21.Linux shell command screen All In One22.Linux shell `#!` interpreter All In One23.Linux shell set command All In One24.Linux shell script auto generate batch files All In One25.Linux bash script HereDoc All In One26.Linux shell system environment variables All In One27.Linux shell command curl All In One28.Linux shell command chroot All In One29.Linux shell command chmod All In One30.Linux shell number variables add All In One31.Linux bash shell "${1}" All In One32.Linux xattr shell command All In One33.Multiple Ways to Change Terminal Shell in Linux All In One34.Linux shell stdin & stdout & stderr All In One35.Linux shell man command All In One36.Linux shell command line editor All In One37.Linux shell command uname All In One38.Linux bash shell script block comment All In One39.Linux bash shell script batch download files All In One40.Linux shell script bug and error All In One41.Linux bash write long string with multi-lines comments All In One42.Linux set command All In One43.Linux echo command All In One44.Linux echo 换行 All In One45.Linux Bash shell 脚本定时器 All In One46.Linux System Variables All In One47.linux bash shell auto read write template48.Linux bash script read args All In One49.Linux Bash Script conditions syntax All In One50.Linux Bash Script loop syntax All In One51.Linux shell script All In One52.如何通过 Linux terminal 查看一个文件的 meta 信息 All In One53.Linux shell uname -a All In One54.Linux shell command create file methods All In One55. how to write string to file in linux bash All In One56.linux bash shell & lsof & grep & ps57.Linux Bash Shell All In One58.Linux shell command copy file All In One59.Linux shell command & zip & tar All In One60.Linux shell command show project directory tree All In One61.Linux shell commands man chmod All In One62.Linux & bash & shell & PID & PPID63.Linux bash shell All In One64.Linux shell command symbolic link & soft link All In One65.Linux bash shell All In One66.Linux shell (.sh 文件) 基础命令 常用命令 汇总
67.How to use a shell script to check whether a command had been installed in the Linux server All In One
68.How to print a string with a variable by using the echo command in the shell script All In One69.Linux install vim errors All In OneHow 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 $
/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-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17721396.html
未经授权禁止转载,违者必究!
合集:
Linux Shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2022-09-22 js Object key All In One
2022-09-22 How to exit Node.js REPL environment All In One
2021-09-22 js scroll page's dom in to view All In One
2020-09-22 二叉搜索树 & 二叉树 & 遍历方法 All In One
2020-09-22 js swap array
2020-09-22 社保卡跨省异地结算
2016-09-22 MyScript©计算器 (数学公式手动输入后自动转换成标准Math的格式)