如何使bash_profile函数在bash_profile中运行不同的运行或稍后由用户调用? [英] How to make a bash_profile function acts different running within bash_profile or later called by user?

查看:773
本文介绍了如何使bash_profile函数在bash_profile中运行不同的运行或稍后由用户调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是,在〜/ .profile 中,函数 doit 会说欢迎当用户登录时,但用户在稍后执行 doit 时说出其他的话。

  doit(){
if some_test_here;那么
回声在〜/ .profile中运行,欢迎。
else
echo由用户调用。
fi
}

doit

我认为在Linux上,〜/ .profile 在Mac上对于〜/ .bash_profile 更好。所以我用〜/ .profile 作为例子。 两种方式要传递参数或检查环境。




使用仅供调用使用的参数 .profile

  doit(){
if [ $ {1:-onlogin}-eq onlogin];然后
echo从.profile运行
else
echo由用户调用
fi
}

doit onlogin#from。档案
doit#普通电话






检查由.profile设置的变量的环境

  doit(){
if [$ {_ onlogin}] ;然后
echo从.profile运行
else
echo由用户调用
fi
}

onlogin = 1 doit#来自.profile;值可以是任何非空字符串
doit#普通调用


I mean, in ~/.profile, a function doit will say Welcome when user login, but say other words when user execute doit later.

doit() {
    if some_test_here; then
        echo "Running within ~/.profile. Welcome."
    else
        echo "Called by user."
    fi
}

doit

I think ~/.profile is better on Mac for ~/.bash_profile on Linux. So I use ~/.profile as example.

解决方案

Two ways to are to pass an argument, or to check the environment.


Use an argument that is only used by the call in .profile.

doit () {
    if [ "${1:-onlogin}" -eq onlogin ]; then
        echo "Running from .profile"
    else
        echo "Called by user"
    fi
}

doit onlogin  # from .profile
doit          # ordinary call


Check the environment for a variable set by .profile

doit () {
  if [ "${_onlogin}" ]; then
    echo "Running from .profile"
  else
    echo "Called by user"
  fi
}

onlogin=1 doit    # from .profile; value can be any non-empty string
doit              # ordinary call

这篇关于如何使bash_profile函数在bash_profile中运行不同的运行或稍后由用户调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆