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
    • 最新
    • 标签
主页 / unix / 问题 / 670997
Accepted
Kes
Kes
Asked: 2021-09-29 06:33:20 +0800 CST2021-09-29 06:33:20 +0800 CST 2021-09-29 06:33:20 +0800 CST

如何在 .bashrc 中使用重复代码块

  • 772

我的 .bashrc 中有 4 个函数,它们是 100 行简单代码

除了函数名称和前 3 行代码是变量之外,这些函数是相同的。

如何将 97 行常见的代码提取到一个单独的实例化块中,然后从 4 个函数中的每一个调用该块?

我尝试制作这些常见的 97 行的通用功能块并从一个非常小的函数调用它,但我无法让它工作。

这是顶部有 3 条唯一行的函数,下面是通用代码

function download_podcaster() 
   
######################################################################
##########                                                  ########## 
#   UNIQUE CODE FOLLOWS  #

#set-up the directory variables

dir_zz="/home/user/podcast_author"  
pone_dir="podcast_author"  
downloads_z="~/Downloads"  

##########   UNIQUE CODE ABOVE                              ##########
##########                                                  ########## 
######################################################################


#  vvvvv COMMON CODE BELOW   vvvvvv


   echo; echo "   .... this output is from youtube-dl ..."; echo

   #download the file
   youtube-dl -f 140 --restrict-filenames -o $dir_zz'/%(title)s.%(ext)s' $1


   #make dir if does not aleready exist
   mkdir -p $dir_zz

   #change to the downloads directory
   cd $dir_zz
   echo
   echo "current dir is:                                   "$(pwd)
   echo
  
   #open the downloads location to show the files with pcmanfm 
   #pcmanfm ~/Downloads &
   pcmanfm $dir_zz &> /dev/null
   
   file_name_z=$(youtube-dl --get-filename --restrict-filenames "$1")
   echo; echo "file name from provided by youtube-dl:            "$file_name_z; echo

   #grab the filename from youtube, and parse it into something useful
   #remove 11 digits before end of file name                
   file_name_z=$(echo $file_name_z | sed 's|...........\.mp4$|.mp4|g' | sed 's|...........\.m4a$|.m4a|g' \
       | sed 's|...........\.webm$|.webm|g' \
       | sed 's|,||g' | sed 's|!||g' | sed 's| |_|g'  |  \
       sed 's|-\.||g' | sed 's|webm||g' | sed 's|mp4||g' | sed 's|\?||g').m4a
           #  remove ,      remove !       replace " " with "_"
           #  remove "-."    remove "webm"
   echo; echo "file name after , ! \" \" ? removed:                "$file_name_z; echo

   var1=$(ls -t | grep -E "^[0-9]{3}" | sort | tail -n  1 | cut --bytes=1-3)
   echo; echo "\$var1 3 digit highest number from file set is:    "$var1; echo
   
   sleep .25
   #create the variable to assign the next file number to front of file name
   next_file_number=$(printf "%03d\n" $((10#$var1+5)))
   echo; echo "File number plus 5 is:                            "$next_file_number; echo

   sleep .25
   #new file name with three digit number in front of filename
   file_name_y=${next_file_number}_${file_name_z}
   echo; echo "concatenated filename is \$file_name_y:            ""$file_name_y"; echo


   #move the old file to the new file name
   mv "$file_name_z" "$file_name_y" 
   echo; echo "                                                  ""$file_name_y"; echo


   #plug phone in. Phone mount point in file system can be seen here
   #echo; cd /var/run/user/$UID/gvfs; ls; echo
   #reference
   #https://askubuntu.com/questions/342319/where-are-mtp-mounted-devices-located-in-the-filesystem
   #
   #How to get to the phone directory on the phone if the directory is dynamically allocated on the 
   #reference  
   #https://askubuntu.com/a/454697/624987
   #phone or changes, use this
   #cd /var/run/user/$UID/gvfs; cd * ; cd *; cd Music; mkdir -p $phone_dir; cd $phone_dir; ls
   #       
   #"cd *" changes to the first directory shown
   
   #grab the directory on the phone in which to place the file
   cd /var/run/user/$UID/gvfs; cd * ; cd *; cd Music; mkdir -p $phone_dir; cd $phone_dir
   phone_dir_long_path=$(pwd)


   echo "  ... now copying the file to the phone -->"; echo
   #copy file to phone
   cp $dir_zz/"$file_name_y" "$phone_dir_long_path"
   echo
   
   #open terminal at directory of files
   gnome-terminal --title="test" --command="bash -c '$phone_dir_long_path; ls; $SHELL'"

   echo
   #open the file name with the default app, usually vlc
   xdg-open $dir_zz/$file_name_y &

   echo
}
bash
  • 1 1 个回答
  • 48 Views

1 个回答

  • Voted
  1. Best Answer
    pLumo
    2021-09-29T06:57:35+08:002021-09-29T06:57:35+08:00

    使用位置参数:

    download() {
        fdir_zz="$1"
        pone_dir="$1"
        downloads_z="$3"
    
        ...
    }
    
    download "/home/user/podcast_author" "podcast_author" ~/Downloads
    

    当然你也可以设置函数,这样你就不需要输入这些东西了:

    download_podcaster(){
        download "/home/user/podcast_author" "podcast_author" ~/Downloads
    }
    download_something_else(){
        download ...
    }
    

    选择:

    仅使用一个位置参数,并使用case ... esac语句对其余参数进行硬编码:

    download(){
        case $1 in
            podcaster) 
                fdir_zz="/home/user/podcast_author"
                pone_dir="podcast_author"
                downloads_z="$HOME/Downloads"
                ;;
            something_else)
                fdir_zz="..."
                pone_dir="..."
                downloads_z="..."
                ;;
        esac
    
        ...
    }
    
    download podcaster
    download something_else
    
    • 4

相关问题

  • 通过命令的标准输出以编程方式导出环境变量[重复]

  • 从文本文件传递变量的奇怪问题

  • 虽然行读取保持转义空间?

  • `tee` 和 `bash` 进程替换顺序

  • 运行一个非常慢的脚本直到它成功

Sidebar

Stats

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

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve