If条件中的特殊字符

时间:2015-06-30 10:09:16

标签: unix

我正在尝试编写一个if语句,用于检查特定变量的前两个字符是否以'$$'开头。我尝试了以下但if条件总是评估为true。

Google_Http_Request Object
(
    [batchHeaders:Google_Http_Request:private] => Array
        (
            [Content-Type] => application/http
            [Content-Transfer-Encoding] => binary
            [MIME-Version] => 1.0
        )

    [queryParams:protected] => Array
        (
            [part] => status,snippet
            [uploadType] => resumable
        )

    [requestMethod:protected] => POST
    [requestHeaders:protected] => Array
        (
            [content-type] => application/json; charset=UTF-8
            [authorization] => Bearer XXXXXXXXXXXXXXXX
            [content-length] => 187
            [x-upload-content-type] => video/*
            [x-upload-content-length] => 10201286
            [expect] => 
        )

    [baseComponent:protected] => https://www.googleapis.com//upload
    [path:protected] => /youtube/v3/videos
    [postBody:protected] => {"snippet":{"categoryId":"22","description":"A video tutorial on how to upload to YouTube","tags":["youtube","tutorial"],"title":"A tutorial video"},"status":{"privacyStatus":"unlisted"}}
    [userAgent:protected] => 
    [canGzip:protected] => 
    [responseHttpCode:protected] => 400
    [responseHeaders:protected] => Array
        (
            [x-guploader-uploadid] => XXXXXXXXXXXXXXXXXXXXXXXXXX
            [location] => https://www.googleapis.com/upload/youtube/v3/videos?part=status,snippet&uploadType=resumable
            [vary] => Origin
X-Origin
            [content-type] => application/json; charset=UTF-8
            [content-length] => 468
            [date] => Fri, 10 Jul 2015 09:54:30 GMT
            [server] => UploadServer
            [alternate-protocol] => 443:quic,p=1
        )

    [responseBody:protected] => {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "wrongUrlForUpload",
    "message": "Uploads must be sent to the upload URL. Re-send this request to https://www.googleapis.com/upload/youtube/v3/videos?part=status,snippet&uploadType=resumable"
   }
  ],
  "code": 400,
  "message": "Uploads must be sent to the upload URL. Re-send this request to https://www.googleapis.com/upload/youtube/v3/videos?part=status,snippet&uploadType=resumable"
 }
}

    [expectedClass:protected] => Google_Service_YouTube_Video
    [expectedRaw:protected] => 
    [accessKey] => 
)

有人可以帮助我吗?尝试在if语句中匹配$$时有什么值得注意的吗

1 个答案:

答案 0 :(得分:1)

使用单引号来避免扩展,并在比较时使用双倍。

如果您使用的是bash,则无需使用剪切。你可以用bash本身做到这一点。

var1='$$abc'
var2='$$def'

var1_cut=${var1:0:2}
var2_cut=${var2:0:2}

if [[ "$var1_cut" == '$$' && "$var2_cut" == '$$' ]]
then
  # Do something
else
  echo "$var1"
fi