相关疑难解决方法(0)

这个 shell/Bash 语法是什么:someVariable=someValue someCommand

我的一位同事为我提供了一种我不熟悉的 Bash 语法。我的 Google foo 无法弄清楚它的作用以及为什么/何时应该使用它。

他发给我的命令是这样的:

someVariable=something command
Run Code Online (Sandbox Code Playgroud)

最初,我认为这等效于以下内容:

someVariable=something ; command
Run Code Online (Sandbox Code Playgroud)

或者

someVariable=something 
command
Run Code Online (Sandbox Code Playgroud)

但事实并非如此。例子:

[Jan-03 11:26][~]$ # Look at the environment variable BAZ. It is currently empty
[Jan-03 11:26][~]$ echo $BAZ

[Jan-03 11:27][~]$ # Try running a command of the same format    
[Jan-03 11:27][~]$ BAZ=jake echo $BAZ

[Jan-03 11:27][~]$    
[Jan-03 11:27][~]$ # Now, echo BAZ again. It is still empty:    
[Jan-03 11:27][~]$ echo $BAZ

[Jan-03 11:27][~]$    
[Jan-03 11:28][~]$    
[Jan-03 11:28][~]$ # If we add a semi-colon to …
Run Code Online (Sandbox Code Playgroud)

command-line shell bash environment-variables

27
推荐指数
3
解决办法
2553
查看次数

我什么时候可以使用临时 IFS 进行字段拆分?

在 bash 中,假设您有var=a.b.c.,然后:

$ IFS=. printf "%s\n" $var
a.b.c
Run Code Online (Sandbox Code Playgroud)

但是,这样的用法IFS在创建数组时确实生效:

$ IFS=. arr=($var)
$ printf "%s\n" "${arr[@]}"
a
b
c
Run Code Online (Sandbox Code Playgroud)

当然,这非常方便,但这是在哪里记录的?快速阅读Bash 文档中有关数组或分的部分并没有给出任何指示。对于搜索IFS通过单页文档不提供有关这种效应的任何暗示无论是。

我不确定何时可以可靠地执行以下操作:

IFS=x do something
Run Code Online (Sandbox Code Playgroud)

并预计这IFS会影响场分裂。

bash

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

如何在安装过程中从 shell 脚本选择交互式提示的响应

我正在编写一个 shell 脚本来一次性在我的 Ubuntu PC 上安装所有我需要的应用程序(同时我可以散步或做其他事情)。对于大多数应用程序添加-yapt-get install语句的末尾,已经很好地避免了任何用户参与的需要。我的脚本看起来像这样:

#!/bin/bash
add-apt-repository ppa:webupd8team/sublime-text-3 -y
apt-get update -y
apt-get upgrade -y
apt-get install synaptic -y
apt-get install wireshark -y
Run Code Online (Sandbox Code Playgroud)

虽然我不再需要担心Do you want to continue? [Y/n]Press [ENTER] to continue or ctrl-c to cancel adding it,但问题在于wireshark,它需要响应如下所示的交互式提示:

Wireshark 提示

我怎样才能避免这种强制性干预?

shell ubuntu shell-script interactive wireshark

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

为什么当我更改 ENV 变量并在同一命令中回显它时,我看不到新值?

当我运行时PATH=abc:$PATH echo $PATH,我得到的是旧值PATH,而不是abc:PATH

为什么是这样?我怎样才能得到abc:$PATH

PATH=/mybin:$PATH which python编辑:如果/mybin有 python 可执行文件怎么样?

shell bash environment-variables

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