Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可能重复: 如何测试变量是否为 bash 中的数字?
我是 bash 脚本的新手。需要一个代码来检查一个变量是否是一个数字,如果是,我必须保留它,如果不是,我必须将它重新分配给其他值。我该怎么做?
您的基本if () then ... fi. 这是格式:
if () then ... fi
#!/bin/bash var=2 if [[ $var != 1 ]] then var=5; fi echo $var
请注意,[[...]]格式实际上可以是许多事情之一。
[[...]]
与所有事情一样,如果您想精通它,就必须为之努力。类型
man bash
man test
获取有关您正在使用的工具的手册。从上到下阅读它们,一遍又一遍,再一遍。然后明天同样的事情。