bash命令`cd/usr/local/src`(包括反引号)无效 [英] bash command `cd /usr/local/src` (including backticks) has no effect

查看:286
本文介绍了bash命令`cd/usr/local/src`(包括反引号)无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对bash还是很陌生,所以我不知道这个问题出现在哪里. 但是,我有这个脚本,应该只更改cd,下载tar文件并将其解压缩. 我正在从/home/

I am fairly new to bash, so I don't know where this problem is appearing. But, I have this script, which is supposed to just change cd, download a tar file and extract it. I am applying this command from /home/

#!/bin/bash
`cd /usr/local/src`
`wget http://nginx.org/download/nginx-1.6.2.tar.gz -O nginx-1.6.2.tar.gz`
`tar -xzf  nginx-1.6.2.tar.gz`

因此,此下载tar文件并提取到主目录中,而不是在/usr/local/src

So, this download the tar file and extracts in home, instead of in /usr/local/src

我已经阅读到cd命令可能存在问题,但是我不知道如何解决此问题.

I have read that this might be a problem from the cd command, but I don't know how to fix this.

推荐答案

编辑后,您的脚本现在显示您正在运行

As edited, your script now says that you're running

#!/bin/bash
`cd /usr/local/src`
`wget http://nginx.org/download/nginx-1.6.2.tar.gz -O nginx-1.6.2.tar.gz`
`tar -xzf  nginx-1.6.2.tar.gz`

反引号引起每个命令在子shell中运行;以及每个子shell的输出将被读取,字符串分割并作为第二个命令运行.

The backticks cause each command to be run in a subshell; and each subshell's output to be read, string-split, and run as a second command.

此外,由于cd位于子外壳中,因此其效果不会延续到主外壳中.

Also, because the cd is in a subshell, its effects don't carry over to the main shell.

因此:删除反引号(并确保您的脚本在cd失败的情况下退出;我在这里使用-e标志执行此操作).您应该只在运行:

So: Remove the backticks (and make sure your script exits if the cd fails; I do this here with the -e flag). You should only be running:

#!/bin/bash -e
cd /usr/local/src
wget http://nginx.org/download/nginx-1.6.2.tar.gz -O nginx-1.6.2.tar.gz
tar -xzf  nginx-1.6.2.tar.gz

...也就是说,您的命令不得以`字符开头和结尾.

...which is to say: Your commands must not start and end with ` characters.

这篇关于bash命令`cd/usr/local/src`(包括反引号)无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆