如何使功能可见的bash脚本? [英] how to make functions visible to bash scripts?

查看:60
本文介绍了如何使功能可见的bash脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的.bashrc常规定义的调用的函数。

我有这样的bash脚本,我想使用Groovy。

报告说, ./ batch.sh:7号线:常规:命令未找到

虽然我在脚本的开始的源.bashrc中。

我在想什么?

batch.sh

 #!的/ usr / bin中/ env的庆典
源的〜/ .bashrc
在* .html文件;

    NAME =$ {文件%的.html}
    常规$ name.html未缓​​存/ $ name.mp3
完成的;

.bashrc中的一部分

 功能常规{
    SED -n/< pre> /,/< \\ / pre> / P'$ 1|代替'< pre>' '''< / pre>' ''| hextomp3 - $ 2
}功能hextomp3 {
    回声$ 1
    回声出2 $
    回声切-c10-74 $ 1 | XXD -r -p - $ 2
    削减-c10-74$ 1| XXD -r -p - $ 2
}

输出:

  chaouche @ karabeela〜/下载/ MUSIQUE $ ./batch.sh
./batch.sh:6号线:常规:命令未找到
./batch.sh:6号线:常规:命令未找到
./batch.sh:6号线:常规:命令未找到
./batch.sh:6号线:常规:命令未找到
./batch.sh:6号线:常规:命令未找到
./batch.sh:6号线:常规:命令未找到


解决方案

的/ etc / bashrc中〜/ .bashrc中不会阅读时的不可以在交互模式下运行。

您可能会看到类似的东西。

 案例$  - 中
    *一世*) ;;
      *)返回;;
ESAC

  [-z$ PS1]&放大器;&安培;返回

〜/ .bashrc中

(如果后者存在的话)

考虑加入你的函数〜/ .profile文件〜/ .bash_profile中

I have a function called groovy defined in my .bashrc.

I have this bash script where I want to use groovy.

It says ./batch.sh: line 7: groovy: command not found

Although I source .bashrc in the beginning of the script.

What am I missing ?

batch.sh

#!/usr/bin/env bash
source ~/.bashrc
for file in *.html;
do
    name="${file%.html}"
    groovy "$name.html" "uncached/$name.mp3"
done;

part of .bashrc

function groovy {
    sed -n '/<pre>/,/<\/pre>/p' "$1" |  replace '<pre>' '' '</pre>' '' | hextomp3 - "$2"
}

function hextomp3 {
    echo "in $1"
    echo "out $2"
    echo "cut -c10-74 $1 | xxd -r -p - $2"
    cut -c10-74 "$1" | xxd -r -p - "$2"    
}

output :

chaouche@karabeela ~/DOWNLOADS/MUSIQUE $ ./batch.sh
./batch.sh: line 6: groovy: command not found
./batch.sh: line 6: groovy: command not found
./batch.sh: line 6: groovy: command not found
./batch.sh: line 6: groovy: command not found
./batch.sh: line 6: groovy: command not found
./batch.sh: line 6: groovy: command not found

解决方案

/etc/bashrc, ~/.bashrc are not read when not running in an interactive mode.

You might see something similar to

case $- in
    *i*) ;;
      *) return;;
esac

or

[ -z "$PS1" ] && return

in your ~/.bashrc.

Consider adding your function to ~/.profile or to ~/.bash_profile (if the latter exists).

这篇关于如何使功能可见的bash脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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