代码重定向到不同的bash行

问题描述 投票:0回答:3

删除并删除主要问题

bash
3个回答
0
投票

您可以定义 shell 函数:

#!/bin/sh

custom_function () {
   echo 'custom_function called !'
}

custom_function

在您的情况下,您可以定义三个不同的函数,每个函数对应一个构建目标,然后调用它们:

#!/bin/sh

target_debian () {
   echo 'debian'
}
target_fedora () {
   echo 'fedora'
}
target_arch () {
   echo 'arch'
}

PS3='Choose your building platform: '
OS=("Debian" "Fedora" "Arch Linux" "Quit")
select fav in "${OS[@]}"; do
    case $fav in
        "Debian")
            target_debian
            ;;
        "Fedora")
            target_fedora
            ;;
        "Arch Linux")
            target_arch
            ;;

            "Quit")
            echo "Aborting..."
            exit
            ;;
       *) echo "invalid option $REPLY";;
    esac
done    

来源:https://www.tutorialspoint.com/unix/unix-shell-functions.htm


0
投票

一种方法是设置一些标志。比如:

#!/usr/bin/env bash

Arch_flag=0
Debian_flag=0
Fedora_flag=0

PS3='Choose your building platform: '
OS=("Debian" "Fedora" "Arch Linux" "Quit")
select fav in "${OS[@]}"; do
  case $fav in
    "Debian")
      Debian_flag=1
      break
      ;;
    "Fedora")
      Fedora_flag=1
      break
      ;;
    "Arch Linux")
      Arch_flag=1
      break
      ;;
    "Quit")
      echo "Aborting..."
      exit
      ;;
    *) echo "invalid option $REPLY";;
  esac
done


if ((Debian_flag)); then
  printf 'Do something with %s\n' Debian
fi

if ((Fedora_flag)); then
  printf 'Do something with %s\n' Fedora
fi

if ((Arch_flag)); then
  printf 'Do something with %s\n' Arch
fi

或者根据用例,可以将标志嵌入循环中。

#!/usr/bin/env bash

PS3='Choose your building platform: '
OS=("Debian" "Fedora" "Arch Linux" "Quit")
select fav in "${OS[@]}"; do
  Arch_flag=0
  Debian_flag=0
  Fedora_flag=0

  case $fav in
    "Debian")
      Debian_flag=1
      ;;
    "Fedora")
      Fedora_flag=1
      ;;
    "Arch Linux")
      Arch_flag=1
      ;;
    "Quit")
      echo "Aborting..."
      exit
      ;;
    *) echo "invalid option $REPLY";;
  esac

  if ((Debian_flag)); then
    printf 'Do something with %s\n' Debian
  fi

  if ((Fedora_flag)); then
    printf 'Do something with %s\n' Fedora
  fi

  if ((Arch_flag)); then
    printf 'Do something with %s\n' Arch
  fi
done

0
投票

I *VERY STRONGLY* encourage you to pay attention to William Pursell's comments
,并使用areop-enap的functions解决方案,或者Jetchisel的简单块,两者都非常简单和优雅。不要陷入为了奇思妙想而嵌入的陷阱。

如果您想将多个文件包含在一个文件中,请使用

tar
等工具。如果它必须是可执行文件,请尝试
zip


但是,在某些(少数)情况下,让脚本完全包含整个数据文件是有用的。

对于那些罕见需要将另一个整个文件编码到脚本中的情况,请将文件放入类似base64编码的文件中。在运行时将您需要的解码为它们自己的文件,甚至解码为管道。 (不要使用偏移。我不得不面对那个噩梦......)

下面的代码只解析它需要的文件。如果您要求“perl”,它甚至不会命中嵌入 bash 文件的部分。 (我不确定解释器如何预解析和优化,但我不认为这种方法会像将这些内存分配给变量那样分配保留内存。) 此方法的一个优点是,它可以让您将任意可执行文件(例如已编译的 C 代码)直接嵌入到脚本中,但这“充其量”是一个有点可疑的优点...

$: ls -l total 1 -rwxr-xr-x 1 paul 1049089 398 Dec 12 08:54 tst $: for arg in bash perl; do ./tst $arg; done ./file1_bash this is the bash file ./file2_perl this one is perl $: ls -l total 3 -rwxr-xr-x 1 paul 1049089 62 Dec 12 08:56 file1_bash -rwxr-xr-x 1 paul 1049089 51 Dec 12 08:56 file2_perl -rwxr-xr-x 1 paul 1049089 398 Dec 12 08:54 tst $: cat file1_bash #! /usr/bin/bash echo "${BASH_SOURCE:-$0}" "$@" $: cat file2_perl #! /usr/bin/perl print join ' ', $0, @ARGV, "\n"; $: cat tst #! /usr/bin/bash case $1 in bash) base64 -d <<< " IyEgL3Vzci9iaW4vYmFzaAplY2hvICIke0JBU0hfU09VUkNFOi0kMH0iICIkQCIgCg== " > file1_bash && chmod +x ./file1_bash && ./file1_bash this is the bash file ;; perl) base64 -d <<< " IyEgL3Vzci9iaW4vcGVybApwcmludCBqb2luICcgJywgJDAsIEBBUkdWLCAiXG4iOyAK " > file2_perl && chmod +x ./file2_perl && ./file2_perl this one is perl ;; esac 还有很多其他问题需要解决 -

解码大文件可能需要很多时间,但脚本应该没问题

如果您正在处理各种操作系统,请注意路径、语法等
  • 确保将 base64 正确复制到文件中,没有损坏(例如缩进)
  • 确保
  • 目标
  • 系统具有正确的工具来解码和运行脚本
  • 创建时确保文件位置和权限正确 如果您不需要留下它们以供以后使用,请考虑自行清理
  • 你明白了。
  • 郑重声明,以防万一,这是在 Windows 下的 GitBash 上完成的。您的版本可能需要进行一些改进。

© www.soinside.com 2019 - 2024. All rights reserved.