1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > ./ . 和#!/bin/bash 辨析Linux如何选择当前执行脚本的shell

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

时间:2023-06-20 09:14:49

相关推荐

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

最近遇到一个有趣的问题,让我搞清楚Linux如何选择当前执行脚本的shell

执行cts的的 media stress test 需要安装android-cts-media-1.0.zip

把这个文件解开,发现有一个copy_media.sh 脚本,就是用 adb push 向 android devices 复制文件,很简单的一个脚本,执行起来却有错

$./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 operatorUsage: 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

执行就没有问题, 为何?我的猜想是:

./ 是用 /bin/sh 执行 copy_media.sh , 它比较”原始“,因此解释不了 其中的 if [ ... ] 语句. 用 . 就是在当前的 bash 下执行,所以成功了。

下面验证我猜想是否正确

我用 $env | grep SHELL输出是bash,那为何 copy_media.sh 是用 /bin/sh 执行

SHELL=/bin/bash

再看文件开始部分,真相大白,原来 copy_media.sh 指定用 /bin/sh执行

head copy_media.sh #!/bin/sh# Copyright (C) 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##/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software

而用 . (就是 source 命令) 绕开了 #!/bin/sh;另一种方法是删除这行,就会使用$SHELL变量执行的shell;当然改成 #!/bin/bash 也可以解决问题。

总结:

Linux 解释脚本按使用 shell 的顺序是

1) 脚本第一行中 #!指定

2) 没有指定就使用 $SHELL

而使用 . 就是在当前 shell 中执行,当然就不会关注 !#/bin/sh 中的指定了.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。