简体   繁体   English

Bash 脚本失败,路径名中有空格

[英]Bash script fails with spaces in path names

I have a shell script I'd like to run, but it fails in accessing the external drive path with spaces.我有一个我想运行的 shell 脚本,但它无法访问带有空格的外部驱动器路径。 I'm not sure which escaping characters to use here.我不确定在这里使用哪些转义字符。 I've tried hard escape \\ and quotes.我已经尝试过转义 \\ 和引号。 The error I get is:我得到的错误是:

ls: /Volumes/G-DRIVE: No such file or directory
ls: R-Series/Sentinel-1/path_82_frame_648_example/*.zip: No such file or directory
ls: SSD: No such file or directory
ls: mobile: No such file or directory

The original script is:原来的脚本是:

#!/bin/sh
Path_S1=/Volumes/'G-DRIVE mobile SSD R-Series'/Sentinel-1/path_82_frame_648_example/   
Path_sub=/Volumes/'G-DRIVE mobile SSD R-Series'/Sentinel-1/path_82_frame_648_sub/   

oldEnd=.zip
newEnd=subset_Orb

for i in $(ls -d -l $Path_S1$S1*.zip)
do
n=${i%.*}
n=${i%T*}
n=${n#"${n%_*}_"}

date
/Applications/snap/bin/gpt /Users/brbell01/Documents/SNAP/subset_and_update_orbits_gpt.xml -Pinput1=$i -Poutput1="$Path_sub$n$newEnd"
date

done

The main problems:主要问题:

A bit of a rewrite for readability:为提高可读性进行了一些重写:

PATH="/Applications/snap/bin:$PATH"

root='/Volumes/G-DRIVE mobile SSD R-Series/Sentinel-1'
Path_S1="$root/path_82_frame_648_example"
Path_sub="$root/path_82_frame_648_sub"

data=/Users/brbell01/Documents/SNAP/subset_and_update_orbits_gpt.xml
oldEnd=.zip
newEnd=subset_Orb

for zip in "$Path_S1"/*"$oldEnd"    # don't quote the wildcard
do
    # do something here to extract $n

    date
    gpt "$data" -Pinput1="$zip" -Poutput1="$Path_sub/${n}$newEnd"
    date
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM