标签: bash

我可以在不安装 Insider Preview 的情况下在 Windows 中启用新的 Ubuntu/bash 吗?

微软已经宣布 bash 和 Unbuntu 将登陆 Windows。据我了解,我可以等待今年夏天的周年更新或安装 Windows 10 的 Insider Preview 才能安装 bash 功能。

有没有办法现在就可以在我的非预览版 Windows 中安装它?

windows bash ubuntu

2
推荐指数
1
解决办法
2674
查看次数

for循环中的Bash增量变量

我有以下几点:

    #!/bin/bash

    a=0
for d in ./*/ ; do (
cd "$d"
((a++))
echo $a
); done
Run Code Online (Sandbox Code Playgroud)

它进入我路径中的每个目录,递增a并打印a。但是,输出始终为 1。这是为什么呢?

bash

2
推荐指数
1
解决办法
2万
查看次数

按“行内容”频率对文件进行排序

假设有 3 个文本文件。

1.txt:

a
b
c
Run Code Online (Sandbox Code Playgroud)

2.txt:

f
c
d
Run Code Online (Sandbox Code Playgroud)

3.txt:

b
c
f
Run Code Online (Sandbox Code Playgroud)

如何按每个“行内容”的频率对它们进行排序?(如果按字母顺序发生碰撞)

结果:

c
b
f
a
d
Run Code Online (Sandbox Code Playgroud)

bash sorting

2
推荐指数
1
解决办法
1814
查看次数

如何从Linux中的文件中grep多行

我有一个包含以下内容的文件。

.
.
hello
.
.
.
world
.
.
hello
.
.
.
.
.
world
.
.
Run Code Online (Sandbox Code Playgroud)

点表示文件中的其他行。这里我只需要 gerp hello 和 world 行。这意味着输出应该如下所示。

hello
world
hello
world
Run Code Online (Sandbox Code Playgroud)

如何做到这一点?

谢谢,

linux bash administrator ubuntu bash-scripting

2
推荐指数
1
解决办法
6609
查看次数

多文件 BASH 脚本的最佳实践?

我正在做的是这样的:

我想将我的脚本分成多个块。在一个地方定义所有变量的最佳实践是什么?我应该在 init 脚本中将它们全部定义并导出,还是有更好的方法让我的脚本的每一部分都查看一个静态变量文件?也许是 JSON 之类的?这是一个例子

项目/main.sh

#!/bin/bash

new_user="poopmacscoop"
a_list_of_packages="pyhon3 nmap apache2.... others"
some_other_info_for_another_script="important info"

menu () {
    read # blah blah whatcha wanna run?
    # other bits
}   
opts () {
    case ... # a bunch of options
    1) ./src/another_script.sh # one of a bunch of scripts in a separate folder
}
while true
do
   menu
   opts
done
Run Code Online (Sandbox Code Playgroud)

现在在 ./src/another_script.sh 脚本中,为了轻松和跳转能力处理 vars 的最佳方法是什么?(这是一个技术术语)

bash bash-scripting

2
推荐指数
1
解决办法
2075
查看次数

从 Bash for Windows 10 运行 Win32 程序

我从 Windows 获得了添加 bash 的最新更新,我试图通过它编写 Android 模拟器的执行脚本;但是,bash 不考虑 Windows 的 Path 变量。

另外,我尝试手动执行 adb 并得到:

andtest@DESKTOP:/mnt/c/Android/sdk/platform-tools$ exec ./adb.exe
bash: /mnt/c/Android/sdk/platform-tools/adb.exe: cannot execute binary file: Exec format error
Run Code Online (Sandbox Code Playgroud)

有什么我在这里想念的吗?我想将此 Windows 计算机用作 Android 的测试服务器。

bash android windows-10 windows-subsystem-for-linux

2
推荐指数
1
解决办法
9280
查看次数

.bashrc 在未经我同意的情况下运行别名:致命:不是 git 存储库(或任何父目录):.git

我在 bash_profile 中有这个,我已经将罪魁祸首缩小到:

alias initialpush="git push -u origin `git symbolic-ref --short -q HEAD`"
Run Code Online (Sandbox Code Playgroud)

我无法正确获取 bash_profile:

cchilders:~ 
$ src
fatal: Not a git repository (or any of the parent directories): .git
Run Code Online (Sandbox Code Playgroud)

更改为其他样式无济于事,同样的问题:

alias initialpush="git push -u origin $(git symbolic-ref --short -q HEAD)"
Run Code Online (Sandbox Code Playgroud)

我认为这只会发生在 Mac 上,我不能确定,直到我回家检查。我的脚本是在 Ubuntu 上编写的。在我使用 Mac 之前,我不记得发生过这个错误。我怎样才能阻止这个 bash_profile 发疯?谢谢

bash git ubuntu .bash-profile macos

2
推荐指数
1
解决办法
438
查看次数

gnu 并行删除命令中空格字符前的转义

我目前正在并行测试 gnu 以使用 bash 在多个服务器之间分发比较命令。在其最基本的功能中,此比较命令需要两个输入进行比较(oracle 数据库访问),并需要通过 -o 输出文件名。程序至少需要一个动作加载、保存或直接上传。

compare -o cmp.input1.input2.dat Input1 Input2
Run Code Online (Sandbox Code Playgroud)

我有几千个这样的输入对并创建一个包含所有组合的文件,以便每一行都包含程序所需的输出文件名和数据库标识符

#test_parallel
-o cmp.input1.input2.dat Input1 Input2
-o cmp.input1.input3.dat Input1 Input3
-o cmp.input2.input3.dat Input2 Input3
[...]
Run Code Online (Sandbox Code Playgroud)

并使用并行执行命令,但是比较命令失败

parallel -a test_parallel "compare {}"
ERROR: No action specified for results (load, save or direct upload)
usage: compare [-u][-o <file>] query target
Run Code Online (Sandbox Code Playgroud)

使用--dryrun模式这是并行执行的:

compare -o\ cmp.input1.input2.dat\ Input1\ Input2
Run Code Online (Sandbox Code Playgroud)

由于某种原因,我不明白,比较程序没有正确处理转义的空格。在 bash 中执行此命令会导致完全相同的错误消息消息。在 -o 标志之后删除转义符(我可以将 -o 移动到并行命令)会导致“参数过多”错误。删除所有转义符会按预期执行命令。

是否可以告诉 parallel 在命令调用中不打印转义符?我似乎没有在文档中找到任何内容,除了这是预期的默认行为,如parallel --shellquote

bash command-line gnu-parallel

2
推荐指数
1
解决办法
1450
查看次数

如何递归地修改所有文件夹,排除特定文件夹中的所有文件夹?

我想要chmod特定文件夹中的所有文件夹和子文件夹,但我希望排除一个文件夹(及其包含的所有子文件夹)。

到目前为止,我对 StackOverflow 中的以下解决方案进行了破解:

这是我到目前为止的想法:

find . -type d \( -path ./node_modules \) -prune -o -print -exec chmod 644 {}\;
Run Code Online (Sandbox Code Playgroud)

问题是有或没有-print我收到以下错误:

查找:缺少`-exec'的参数

以下行具有我需要-exec chmod 644{}\;从中读取的预期结果:

find . -type d \( -path ./node_modules \) -prune -o -print
Run Code Online (Sandbox Code Playgroud)

我在那条线上缺少什么来管道数据-exec

linux bash permissions chmod find

2
推荐指数
2
解决办法
8135
查看次数

在linux中将每个文件移动到其父目录中

假设我有

dir 1/dir 2/file 1
dir 3/dir 4/file 2
Run Code Online (Sandbox Code Playgroud)

(目录和文件名中的空格是有意的)

我想结束

dir 1/file 1
dir 3/file 2
Run Code Online (Sandbox Code Playgroud)

相关但不完整的答案

此答案将所有文件移动到当前父目录(而不是每个文件的父目录)

find . -maxdepth 1 -exec mv {} .. \;
Run Code Online (Sandbox Code Playgroud)

这个答案打印出每个父目录,但我不确定如何移动。

find . -name '.user.log' | xargs -I{} dirname {}
Run Code Online (Sandbox Code Playgroud)

linux bash command-line find

2
推荐指数
1
解决办法
1430
查看次数