AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1347980
Accepted
Jags
Jags
Asked: 2021-06-25 10:03:10 +0800 CST2021-06-25 10:03:10 +0800 CST 2021-06-25 10:03:10 +0800 CST

Bash:在 bashrc 中定义一个可以被全局任何脚本使用的函数

  • 772

我喜欢通过或定义一个函数~/.bashrc并在不同的脚本中使用它。export -fsource .bashrc

功能:

nano ~/.bashrc

function test_func() {
    yt-dlp -f '299+140' --merge-output-format mp4 -cia List.txt;
}

export -f test_func

剧本:

#!/bin/bash

cd /home/admn/Downloads/YT_DL;

test_func --autonumber-start 101 -o '%(autonumber)1d_%(title)s.%(ext)s';

问题一:

之后test_func,命令的其余部分--autonumber-start 101 -o '%(autonumber)1d_%(title)s.%(ext)s'根本不起作用。



问题2:

早些时候,当我尝试source .bashrc在我的脚本中使用时,我收到了以下错误:

/usr/local/scripts/test.sh: line 3: .bashrc: No such file or directory
/usr/local/scripts/test.sh: line 12: test_func: command not found

函数(不带export -f):

nano ~/.bashrc

function test_func() {
    yt-dlp -f '299+140' --merge-output-format mp4 -cia List.txt;
}

脚本(带source .bashrc):

#!/bin/bash

source .bashrc

cd /home/admn/Downloads/YT_DL;

test_func --autonumber-start 101 -o '%(autonumber)1d_%(title)s.%(ext)s';

编辑1:

$ bash -xv /usr/local/scripts/test.sh
#!/bin/bash

source /home/admn/.bashrc
+ source /home/admn/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# alias pip='pip3.7'
alias python='python3'
++ alias python=python3

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac
++ case $- in
++ return

test_func --autonumber-start 101 -o '%(autonumber)1d_%(title)s.%(ext)s';
+ test_func --autonumber-start 101 -o '%(autonumber)1d_%(title)s.%(ext)s'
/usr/local/scripts/test.sh: line 5: test_func: command not found
$ 

这些是我经历过的一些线程;虽然我有一些想法,但我仍然无法为我的用例找到解决方案。谢谢。

https://unix.stackexchange.com/questions/63665/how-to-define-a-bash-function-that-c​​an-be-used-by-different-scripts

https://stackoverflow.com/questions/6218268/how-to-define-a-bash-function-for-use-in-any-script

https://stackoverflow.com/questions/17219174/variables-set-in-bashrc-and-accessing-them-in-shellscript

https://stackoverflow.com/questions/1500499/how-do-you-call-a-function-defined-in-bashrc-from-the-shell

定义一个可供任何脚本使用的全局可用的 bash 函数

操作系统:Ubuntu 伴侣 21.04

Bash:5.1.4(1)-release (x86_64-pc-linux-gnu)

bash
  • 1 1 个回答
  • 643 Views

1 个回答

  • Voted
  1. Best Answer
    choroba
    2021-06-25T10:12:16+08:002021-06-25T10:12:16+08:00

    如果您希望函数使用参数,则需要明确提及它们。

    function test_func() {
        yt-dlp "$@" -f '299+140' --merge-output-format mp4 -cia List.txt
        #      ~~~~
    }
    

    "$@"代表“所有参数”,您也可以使用位置参数,如"$1","$2"等。

    如果您从不同的目录获取文件,则需要指定完整路径,或者将文件的路径放在$PATH.

    source ~/.bashrc
    # or
    PATH+=:~
    source .bashrc
    

    更新后: 您的.bashrc包含一个return停止处理.bashrc如果未在交互式外壳中运行的。如果您也想在非交互式 shell 中执行函数声明,请将函数声明放在条件之前的某个位置。

    • 4

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve