微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用内置 bash

如何解决使用内置 bash

我正在为 Windows 7 编写 bash 脚本并且限制为 bash.exe

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

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

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

这是可用的 bash 内置函数

&(( 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 文件 find futility grep gzip hexdump kernel_dump loki_tool ls lz4 lzop mac2unix mboot md5sum mkboot img mkimage mkmtbootstatrcxd-imgsvc触摸 unix2dos unix2mac unpackbootimg unpackelf xz

注意:我已编辑问题以澄清需求。

解决方法

如果我正确阅读了该 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 安装,而是 bash 目录下的某种简化/不完整/混蛋的 C:\Android\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,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 ... shrug ... 让发起者提取更多的二进制文件(例如,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 中定义的位置/路径...

,

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。