如何在bash中获取csh脚本以设置环境 [英] how to source a csh script in bash to set the environment

查看:359
本文介绍了如何在bash中获取csh脚本以设置环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在Solaris上运行了Oracle,默认情况下外壳为csh.因此,登录脚本还在csh中设置了oracle_home和oracle_sid.但是我不喜欢csh,并且想要使用bash来完成我的工作.那么如何在bash中获取csh登录脚本的源代码呢?

We have Oracle running on Solaris, and the shell is by default csh. So the login script sets the oracle_home, oracle_sid in csh also. But I don't like csh and want to use bash to do my work. So how to source the csh login script in bash?

例如,.cshrc文件中的内容如下.当使用bash时,我想使用这些变量.一种方法是再次复制变量并使用bash命令,例如export ORACLE_SID = TEST.但是,这样做将需要我们维护文件的两个副本.当我们更改数据库名称或升级数据库时,我需要单独维护bash登录文件.只需使用

e.g, the following is what in the .cshrc file. And when use bash, I'd like use these variables. One way is to copy the variables again and use bash command, such as export ORACLE_SID=TEST. But doing so will require us to maintain two copies of the files. And when we change the database name, or upgrade the database, I need to maintain the bash login file separately. It's nice to just use something like

bash中的.cshr源,但是它不起作用.

source .cshr in bash, but it doesn't work.


setenv ORACLE_SID TEST
setenv ORACLE_HOME /oracle/TEST/home/products/10204
setenv EPC_DISABLED TRUE
setenv MANPATH /usr/local/man:/usr/share/man
setenv EDITOR vi
setenv LD_LIBRARY_PATH $ORACLE_HOME/lib:/usr/sfw/lib/64
setenv NLS_LANG AMERICAN_AMERICA.UTF8
setenv NLS_DATE_FORMAT "DD-MON-RR"

推荐答案

在您的~/.bashrc(或存在的~/.bash_profile~/.bash_login~/.profile的第一个)中,该脚本使用类似于:

In your ~/.bashrc (or the first of ~/.bash_profile, ~/.bash_login, and ~/.profile that exists) source this script using something like . ~/bin/sourcecsh:

#!/bin/bash
# This should be sourced rather than executed
while read cmd var val
do
    if [[ $cmd == "setenv" ]]
    then
        eval "export $var=$val"
    fi
done < ~/.cshrc

此版本消除了邪恶的eval:

#!/bin/bash
# This should be sourced rather than executed
# yes, it will be sourcing within sourcing - what so(u)rcery!
source /dev/stdin < \
<(
    while read cmd var val
    do
        if [[ $cmd == "setenv" ]]
        then
             echo "export $var=$val"
        fi
    done < cshrc
)

不采购标准输入:

while read cmd var val
do
    if [[ $cmd == "setenv" ]]
    then
        declare -x "$var=$val"
    fi
done < cshrc

这篇关于如何在bash中获取csh脚本以设置环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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