Shell脚本和路径中的引号 [英] Shell script and quote in path

查看:141
本文介绍了Shell脚本和路径中的引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须很稠密,但是我根本无法解决这个问题.我已经在这里搜索和搜索了,但没有找到有效的方法.

I must be dense, but I simply cannot solve this. I have googled and searched here and nothing I find works.

我有一个简单的shell脚本.目前看起来如下.diff命令失败,第二个参数(下面的红色图标)中显示无此文件或目录".

I have a simple shell script. Currently it looks as below. The diff command fails with "No such file or directory" on the second argument (the one in red below).

#!/bin/bash

set -x

cd "/Volumes/$1/Backups.backupdb/SkiAddict’s Mac/Latest"
time diff -qr /Users/n/Documents/Code "SkiAddict’s Mac/Users/n/Documents/Code"

我在终端中这样调用脚本:

I call the script like this from the Terminal:

bash myScript.sh Quadra

我不太了解shell脚本(实际上一点也不了解),但这真的不是火箭科学!

I don't know shell scripting very well (actually, not well at all) but this really can't be rocket science!

我花了一个多小时将头撞在墙上,使第一个命令(cd)开始工作;原来,除非"SkiAddict's"中的引号字符是智能引号,否则我会遇到相同的错误(没有这样的文件或目录).现在,该行工作正常,即使我从该行复制文本并将其粘贴到diff命令中,也行不通.

I spent more than an hour banging my head against the wall to get the first command (cd) to work; it turned out that unless the quote character in "SkiAddict's" was a smart quote, I got the same error (No such file or directory). Now that line is working fine and even if I copy the text from that line and paste it into the diff command, it doesn't work.

整个脚本是从一个已经正常工作的QuicKeys宏复制的,该宏将文本键入到Terminal窗口中,因此我真的觉得制作一个shell脚本不是什么大问题.宏首先将这一行键入"到新的终端"选项卡中:

This entire script was copied from an already-working QuicKeys macro which types the text into a Terminal window, so I really figured making a shell script wouldn't be a biggie. The macro begins by "typing" this line into a new Terminal tab:

cd /Volumes/Quadra/Backups.backupdb/SkiAddict’s\ Mac/Latest

然后按Enter并在此行中键入":

It then presses Enter and "types" this line:

time diff -qr /Users/n/Documents/Code "SkiAddict's Mac/Users/n/Documents/Code"

再次,然后按Enter键.事情做得很好.

Again, it then presses Enter. The thing works perfectly.

我已经尝试过在两端使用双引号,在两端使用单引号,并在两端使用单引号和不使用双引号来转义.什么都行不通.

I've tried double quotes at either end, single quotes at either end, escaping the quote, both with and without double or single quotes at either end. Nothing works.

帮助?

推荐答案

昨晚我找到了答案,但是由于我是新来的人,所以当时无法回答自己的问题.多亏了 Bash常见问题解答,我才能够了解$'...',从而启用了单引号要反斜杠的路径,这使 diff 命令满意.

I found the answer last night but since I'm new here I couldn't reply to my own question then. Thanks to the Bash FAQ I was able to learn about $'...' This enables the single quote in the path to be backslashed, which makes the diff command happy.

不, cd 命令不能以相同的方式处理.如果我更改该行的任何内容,将无法再使用!!!!叹气...

And no, the cd command cannot be treated in the same way. If I change anything about that line, it doesn't work again!!! sigh...

因此,这两行如下所示:

So, the two lines look like this:

cd "/Volumes/$1/Backups.backupdb/SkiAddict’s Mac/Latest"
time diff -qr /Users/n/Documents/Code $'SkiAddict\'s Mac/Users/n/Documents/Code'

非常感谢@askewchan的帮助.即使我们没有取得任何进展,也很高兴有人可以谈论"它:-)

Many thanks @askewchan for your help. Even though we didn't get anywhere together with it, it was still very nice to have someone to "talk" to about it :-)

这篇关于Shell脚本和路径中的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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