【问题标题】:make directory with bash builtin使用内置 bash 创建目录
【发布时间】:2021-05-31 17:33:01
【问题描述】:

我正在为 Windows 7 编写 bash 脚本,但对 bash.exe 有限制

mkdir -p 不起作用(找不到命令)

我正在寻找任何带有 bash 内置函数的替代品/作弊器。 (-p 标志不是强制性的,可以用循环代替)

Transform a file into directory 有一些想法或其他一些只有专家知道的好技巧。这个问题不是关于如何修复丢失的 mkdir 二进制文件,而是关于减少(损坏的)核心实用程序的依赖关系。

这是可用的 bash 内置函数:

& (( . : [ [[ alias bg bind break builtin caller case cd command compgen complete compopt continue coproc declare dirs disown echo enable eval exec exit export false fc fg for function getopts hash help history if jobs kill let local logout mapfile popd printf pushd pwd read readarray readonly return select set shift shopt source suspend test time times trap true type typeset ulimit umask unalias unset until wait while {

如果无法使用 bash 内置函数,则可以使用一些外部工具。已经有一个例子依赖于cp
Create a new folder using bash without mkdir command
这就是我所拥有的:

bash blobpack blobunpack bzip2 cat chmod clear cpio cut dd dhtbsign dos2unix dumpimage elftool expr 文件查找无用 grep gzip hexdump kernel_dump loki_tool ls lz4 lzop mac2unix mboot md5sum mkbootimg mkimage mkmtkhdr mv printf pxa-mkbootimg pxa-unpackbootimg rkctar rms touch unix2dos unix2mac unpackbootimg unpackelf xz

注意:我已编辑问题以阐明需求。

【问题讨论】:

  • 据我所知,没有内置的 bash 可以创建目录。您必须使用外部程序。
  • 用 mkdir 命令显示你的代码。
  • 获取这个混蛋cygwin/bash 'installation' 的创建者以添加更多二进制文件(例如,mkdir.exe)或者更好的是,构建自己的(即,安装完整版本的cygwin/bash 和然后选择你想要的二进制文件,用于减少/混蛋cygwin/bash 安装)
  • 我可以自己添加,也可以添加busybox。但我只能在我自己的电脑上做。有很多这样的精简包(例如 Odinatrix),目标是制作具有精简依赖项的通用脚本。也许我正在编写 .bat 文件来创建文件夹,因为 windows mkdir 可以工作

标签: shell terminal cygwin mkdir


【解决方案1】:

如果我正确阅读了 pastebin.com 链接的内容,这不是真正/完整的 cygwin 安装。

该链接显示以下内容(在 Windows 环境中):

PATH = ... C:\Android\bash ...                 # this is not where cygwin is typically installed though, yeah, you could override the default installation directory

C:\Users\mint>bash                             # this is not how `cygwin/bash` is invoked
bash-4.1$ 

bash-4.1$ ls /cygdrive/c/Android/bash          # full cygwin install does not throw everything under a single directory like this:
bash.exe          ... snip ...      mv.exe

此时这似乎不是实际的cygwin 安装,而是C:\Android\bash 目录下的某种简化/不完整/混蛋bash 安装。

OP 在哪里/如何“安装”C:\Android\bash 目录下的内容?

出于比较目的(我的 Windows 机器):

# cygwin installation directory:

C:\cygwin64>dir
 Volume in drive C is Windows7
 Volume Serial Number is xxxx-yyyy

 Directory of C:\cygwin64

11/06/2020  12:29    <DIR>          .
11/06/2020  12:29    <DIR>          ..
03/30/2021  16:08    <DIR>          bin
05/25/2019  17:15            53,342 Cygwin-Terminal.ico
05/25/2019  18:46                95 Cygwin.2.bat
05/25/2019  17:15                88 Cygwin.bat
05/25/2019  17:15           157,097 Cygwin.ico
02/08/2021  13:01    <DIR>          dev
02/04/2021  12:06    <DIR>          etc
02/26/2021  16:35    <DIR>          home
02/02/2021  11:34    <DIR>          lib
07/12/2020  17:28    <DIR>          sbin
11/06/2020  12:29    <DIR>          srv
05/07/2021  07:46    <DIR>          tmp
02/02/2021  11:34    <DIR>          usr
05/25/2019  17:15    <DIR>          var
               6 File(s)        210,622 bytes
              12 Dir(s)  20,305,154,048 bytes free

C:\cygwin64\bin>dir
 Volume in drive C is Windows7
 Volume Serial Number is xxxx-yyyy

 Directory of C:\cygwin64\bin

03/30/2021  16:08    <DIR>          .
03/30/2021  16:08    <DIR>          ..
... snip ...
02/03/2017  14:40            37,395 base64.exe
02/03/2017  14:40            29,715 basename.exe
01/27/2017  14:13           739,859 bash.exe               # bash binary
01/27/2017  14:13             7,291 bashbug
10/17/2014  17:00            81,949 bc.exe
... snip ...
12/20/2020  17:01            11,564 mintheme
02/03/2017  14:40            62,995 mkdir.exe              # mkdir binary
02/03/2017  14:40            29,715 mkfifo.exe
08/22/2020  14:00            21,523 mkgroup.exe
... snip ...
2/19/2020  11:37                30 zstdless
2/03/2017  14:41            64,019 [.exe
           1130 File(s)    541,740,761 bytes               # 1100+ binaries in this directory
              2 Dir(s)  20,305,154,048 bytes free

# example cygwin session startup

C:\cygwin64\bin\mintty.exe -i /Cygwin-Terminal.ico -

     # user has option to startup a few different tty's;
     # actual OS is determined from /etc/passwd entry (/usr/bin/bash in my case)

此时,如果 OP 想要运行 cygwin/bash,那么我建议安装一个实际的 cygwin 环境(请参阅 cygwin.org ),确保还安装 bash 包。

至于如何模拟mkdir ...无论安装在C:\Android\bash下... 耸耸肩 ...让发起者拉出更多的二进制文件(例如,mkdir.exe ) 从完整的cygwin/bash 安装?

----------- 上一个答案(在查看pastebin.com 链接的内容之前)

我想知道这是否可能是cygwin/bash 安装不完整/损坏或$PATH 无效的问题...?

来自我的cygwin 环境:

$ bash --version
GNU bash, version 4.4.12(3)-release (x86_64-unknown-cygwin)

$ which mkdir
/usr/bin/mkdir

$ command -v mkdir
/usr/bin/mkdir

$ mkdir --version
mkdir (GNU coreutils) 8.26
Packaged by Cygwin (8.26-2)

/usr/bin/mkdir 存在吗?

运行find / -name mkdir*时返回什么?

如果您能找到mkdir(.exe),那么下一次检查将是在$PATH 中定义的位置/路径...

【讨论】:

  • 那么这听起来像是一个不完整的cygwin 和/或cygwin/bash 安装,因为/usr/bin(和/usr/bin/mkdir)应该存在。
  • thx 但问题是关于 mkdir 替代而不是关于 cygwin 核心实用程序。我已经编辑了问题并删除了误导性词 cygwin
  • 主要问题仍然存在......有人从cygwin/bash 安装中删除了一个选择的二进制文件列表,然后将它们猛烈撞击到一个目录中;因为在一个完整的cygwin 安装中有数千个二进制文件,您最好转到这个混蛋 cygwin 安装的源并要求他们添加更多的二进制文件(例如,mkdir.exe
  • 如果您要将脚本推送到远程机器......为什么不推送您需要的额外cygwin/bash 二进制文件?
  • 是的,这可能是最简单的解决方法,只需将 x86 busybox 捆绑在一个 zip 中 - 会去的,谢谢!
【解决方案2】:

这个技巧有助于在 bash 脚本中运行 Windows md shell 内置

echo -e 'md %*\r' > mkdir.bat
./mkdir.bat test

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 2014-06-16
    • 1970-01-01
    相关资源
    最近更新 更多