shell经典函数

18 篇文章 0 订阅

获取脚本所在路径

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

echo $DIR

不成功则退出

function exit_if_not_success()
{
    if [ $? -eq 0 ] ;then
        echo "success"
    else 
        exit 1
    fi
}

# $? 用来获取上一个命令的退出状态,或者上一个函数的返回值
# 退出状态是一个数字
# 一般情况下命令执行成功会返回 0,失败返回 1,这和C语言的 main() 函数是类似的。

上述函数的功能类似于set -e命令

#!/bin/sh
set -e
# Exit immediately if a simple command exits with a non-zero status

根据历史命令前缀查找补全命令

在bashrc中添加一下代码

if [[ $- == *i* ]]; then
        bind '"\e[A": history-search-backward'
        bind '"\e[B": history-search-forward'
fi

判断某个命令是否存在

The following is a portable way to check whether a command exists in PATH and is executable:[ -x "$(command -v foo)" ]

Example:

if ! [ -x "$(command -v git)" ]; then
  echo 'Error: git is not installed.' >&2
  exit 1
fi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值