标签: shell

shell脚本无法将命令行的输出保存到变量中

我正在尝试执行shell命令,然后使用shell脚本将输出保存到变量中.所以我使用这样的反引号:

out=`ls -l`
print $out
Run Code Online (Sandbox Code Playgroud)

该代码工作正常,我可以将它用于任何其他shell命令,但当我尝试做'python --version'来检查python版本.我有一个奇怪的问题.反引号不适用于"python --version"命令,当它与任何其他命令一起正常工作时,它将输出打印到屏幕并且不向变量$ out保存任何内容.所以,如果我这样做:

out =`python --version`
print "my python version is "$out
Run Code Online (Sandbox Code Playgroud)

结果:

Python 2.6.1
my python version is 
Run Code Online (Sandbox Code Playgroud)

我也尝试用$()替换反引号,但没有运气.任何的想法 ?

variables shell command-line backticks

3
推荐指数
1
解决办法
3930
查看次数

Shell参数扩展:如何在没有目录部分的情况下获取文件名?

我正在写一个makefile,并假设我有以下内容;

FILES = file1.py \
    folder1/file2.py \
    folder2/file3.py
Run Code Online (Sandbox Code Playgroud)

我有以下for循环:

    -@for file in $(FILES); do \
           echo $${file/folder1\/}; \
    done
Run Code Online (Sandbox Code Playgroud)

以上将打印:

file1.py
file2.py
folder2/file3.py
Run Code Online (Sandbox Code Playgroud)

我想要的输出是:

file1.py
file2.py
file3.py
Run Code Online (Sandbox Code Playgroud)

我查看了一些关于shell扩展的文档,但还没有找到解决这个问题的方法.我可以知道如何更改代码以获得正确的输出吗?任何帮助将非常感谢.

编辑:语法

shell makefile variable-expansion

3
推荐指数
1
解决办法
1948
查看次数

Bourne shell:强制STDOUT到STDERR

我正在创建一个Subversion post-commit钩子.我需要强制将一个命令的输出强制转换为STDERR,这样后提交挂钩会将消息封送回客户端.

如何强制STDOUT到STDERR?

在此简化示例中,文件foo存在,但bar不存在.

# touch foo
# ls foo bar
ls: bar: No such file or directory
foo
Run Code Online (Sandbox Code Playgroud)

我想将STDOUT发送给STDERR.我以为我可以这样做>&2,但它似乎没有起作用.

我期待以下示例将STDOUT重定向到STDERR,这/tmp/test将包含命令的输出和错误.但相反,似乎STDOUT永远不会重定向到STDERR,因此/tmp/test只包含命令的错误.

# ls foo bar >&2 2>/tmp/test
foo
# cat /tmp/test
ls: bar: No such file or directory
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

我在CentOS,Ubuntu,FreeBSD和MacOSX上尝试了上面的例子.

shell sh

3
推荐指数
1
解决办法
2320
查看次数

Applescript Shell脚本进展

我正在创建一个AppleScript,它在Mac Os X中创建/ Users文件夹的备份图像.我想做两件事之一:

  1. 在shell脚本运行时显示理发杆进度条,并选择退出.
  2. 在脚本运行时显示一个对话框,并带有退出选项.

我尝试使用/ dev/null执行shell脚本但忽略所有输出.我想保存输出并将其显示在用户的对话框中.

这是我的代码:

set computername to (do shell script "networksetup -getcomputername")
set fin to ""
set theIcon to note
tell application "Finder"
    try
        set folderalias to "/Users"
        set foldersize to physical size of folder (alias ":Users") --(info for folderalias) 
        set foldersize to (round ((foldersize / 1.0E+9) * 100)) / 100
    on error
        tell application "System Events"
            set foldersize to (round (((get physical size of folder ":Users" as text) / 1.0E+9) * 100)) / 100 …
Run Code Online (Sandbox Code Playgroud)

shell applescript progress

3
推荐指数
1
解决办法
8239
查看次数

当子命令抛出时终止makefile命令?

我有以下Makefile:

#runs the working directory unit tests
test:
    @NODE_ENV=test; \
        mocha --ignore-leaks $(shell find ./test -name \*test.js);

#deploys working directory
deploy:
    @make test; \
    make deploy-git; \
    make deploy-servers;

#deploys working to git deployment branch
deploy-git:
    @status=$$(git status --porcelain); \
    if test "x$${status}" = x; then \
        git branch -f deployment; \
        git push origin deployment; \
        echo "Done deploying to git deployment branch."; \
    else \
        git status; \
        echo "Error: cannot deploy. Working directory is dirty."; \
    fi …
Run Code Online (Sandbox Code Playgroud)

shell makefile

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

在linux bourne shell中:如何计算文件中特定单词的出现次数

通过语言,我的意思是任何以空格分隔的字符串.

假设文件test.txt具有由空格分隔的以下单词:

hello hello hello hell osd
hello
hello 
hello
hellojames beroo helloooohellool axnber hello
way
how 
Run Code Online (Sandbox Code Playgroud)

我想计算每行中出现单词hello的次数.

我使用该命令awk -F "hello" '{print NF-1}' test.txt显示每行中单词hello的出现次数:

3
1
1
1
4
0
0
Run Code Online (Sandbox Code Playgroud)

因此它总共发现了3 + 1 + 1 + 1 + 4 = 10次.

问题是在第四行:你好只发生一次作为一个单独的单词; 词如hellojames和helloooohellool不应计入因为喂不是由空格分隔.

所以我希望它找到7次出现的hello作为一个单独的单词.

你能帮我写一个返回正确总共7次的命令吗?

unix shell awk grep

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

Git文件重命名产生错误:"警告:目标存在;将覆盖!"

我想在本地存储库中重命名我的文件.所以我这样做:

git mv -f hashpq.py HashPQ.py
Run Code Online (Sandbox Code Playgroud)

得到这个:

warning: destination exists; will overwrite!
Run Code Online (Sandbox Code Playgroud)

然后我通过以下方式获得git的状态:

git status
Run Code Online (Sandbox Code Playgroud)

得到这个回应:

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    hashpq.py
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   hashpq.py
Run Code Online (Sandbox Code Playgroud)

但在我列出我的目录之后:

ls
Run Code Online (Sandbox Code Playgroud)

我有这个:

CHANGLELOG  hashpq.py  NEXTPLAN  README  TODO
Run Code Online (Sandbox Code Playgroud)

我仍然使用以前的名称更改hashpq.py.如何将文件重命名为HashPQ.py?

git shell

3
推荐指数
1
解决办法
1689
查看次数

Bash - 如何在当前文件夹中获取名称文件夹?

假设我的脚本在一个文件夹中运行,并且在这个文件夹中是一个anther文件夹,其名称可以在每次运行脚本时更改.如何找到该文件夹​​的名称?

编辑:

举个例子,假设我的脚本在文件夹中运行,该文件/testfolder已知并且不会更改.

在/testfolder另一个文件夹:/testfolder/randomfolder,它是未知的,可以更改.

我怎么找到这个名字/randomfolder?

我希望它更清楚,抱歉这令人困惑.

directory bash shell

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

unix shell脚本中的cat命令

我有2行代码

1)使用以下代码:

for i in `ls *.properties`; do cat $i; done
Run Code Online (Sandbox Code Playgroud)

我收到错误:

cat: file_name.properties: No such file or directory.
Run Code Online (Sandbox Code Playgroud)

2)另一方面:

for i in *.properties; do cat $i; done
Run Code Online (Sandbox Code Playgroud)

工作良好!

我以为两者都是一样的.有人能帮我理解两者之间的区别吗?使用bash shell.

unix bash shell cat

3
推荐指数
1
解决办法
9235
查看次数

Shell one-liner将一行添加到已排序的文件中

我想在文本文件中添加一行,以便对文本文件最初排序的结果进行排序.例如:

cp file tmp; echo "new line" >> tmp; sort tmp > file; rm -f tmp
Run Code Online (Sandbox Code Playgroud)

我真的很喜欢没有临时文件和没有分号(使用管道代替吗?); 使用sed是可以接受的.这是可能的,如果是的话,怎么样?

sorting shell sed pipe

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