日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

./ . 和#!/bin/bash 辨析Linux如何选择当前执行脚本的shell

發(fā)布時間:2024/4/17 linux 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ./ . 和#!/bin/bash 辨析Linux如何选择当前执行脚本的shell 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

最近遇到一個有趣的問題,讓我搞清楚Linux如何選擇當(dāng)前執(zhí)行腳本的shell


執(zhí)行cts的的 media stress test 需要安裝?android-cts-media-1.0.zip

把這個文件解開,發(fā)現(xiàn)有一個?copy_media.sh 腳本,就是用 adb push 向 android devices 復(fù)制文件,很簡單的一個腳本,執(zhí)行起來卻有錯

$./copy_media.sh 1920x1080

?

1920x1080 ./copy_media.sh: 22: [: 1920x1080: unexpected operator ./copy_media.sh: 25: [: 1920x1080: unexpected operator ./copy_media.sh: 28: [: 1920x1080: unexpected operator ./copy_media.sh: 31: [: 1920x1080: unexpected operator ./copy_media.sh: 34: [: 1920x1080: unexpected operator Usage: copy_media.sh [720x480|1280x720|1920x1080] [-s serial]for testing up to 1280x720, copy_media.sh 1280x720default resolution, when no argument is specified, is 480x360copy_media.sh all will copy all the files.


但如果我用

?

$. copy_media.sh 1920x1080

執(zhí)行就沒有問題, 為何?我的猜想是:

./ 是用 /bin/sh 執(zhí)行 copy_media.sh , 它比較”原始“,因此解釋不了 其中的 if [ ... ] 語句. ?用 . 就是在當(dāng)前的 bash 下執(zhí)行,所以成功了。


下面驗(yàn)證我猜想是否正確

我用 $env | grep SHELL輸出是bash,那為何 copy_media.sh 是用 /bin/sh 執(zhí)行

SHELL=/bin/bash


再看文件開始部分,真相大白,原來 copy_media.sh 指定用 /bin/sh執(zhí)行

?

head copy_media.sh #!/bin/sh # Copyright (C) 2012 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software


而用 . (就是 source 命令) 繞開了 #!/bin/sh;另一種方法是刪除這行,就會使用$SHELL變量執(zhí)行的shell;當(dāng)然改成 #!/bin/bash 也可以解決問題。

?


總結(jié):

Linux 解釋腳本按使用 shell 的順序是

1) ?腳本第一行中 #!指定

2) 沒有指定就使用 $SHELL?

而使用 . 就是在當(dāng)前 shell 中執(zhí)行,當(dāng)然就不會關(guān)注 !#/bin/sh 中的指定了.


?

轉(zhuǎn)載于:https://www.cnblogs.com/james1207/p/3313177.html

總結(jié)

以上是生活随笔為你收集整理的./ . 和#!/bin/bash 辨析Linux如何选择当前执行脚本的shell的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。