shell 脚本 #!/bin/bash #!/usr/bin/env bash 这两个有什么区别啊

作者&投稿:饶炊 (若有异议请与网页底部的电邮联系)
在执行shell脚本时,提示:bash: ./shell.sh: bin/bash: bad interpreter: No such file or directory~

如果确定不是格式问题,而且也不是你的脚本没有执行权限x,那么查查你的shell用的是什么,which bash查看是不是bash。
你到脚本的目录下用 bash shell.sh试一下

#!/usr/local/bin/python
You are specifying the location to the python executable in your machine, that rest of the script needs to be interpreted with.
You are pointing to python is located at /usr/local/bin/python

Consider the possiblities that in a different machine, python may be installed at /usr/bin/python or /bin/python in those cases, the above #! will fail.
For those cases, we get to call the env executable with argument which will determine the arguments path by searching in the $PATH and use it correctly.

Thus,
#/usr/bin/env python
Will figure out the correct location of python ( /usr/bin/python or /bin/python from $PATH) and make that as the interpreter for rest of the script.
- ( env is almost always located in /usr/bin/ so one need not worry what is env is not present at /usr/bin)

其实是没什么区别的。
前面一种最常见、最常用;
后面一种比较少见,主要是考虑到程序的可移植性,其中,env的作用就是为了找到正确的脚本解释器(这里就是bash),在不同的Linux系统上该解释器可能所处的位置不同。


融水苗族自治县13190336365: Linux Shell脚本 -
茆废吾斯: 一般以#!/bin/sh 开头(不是必须要写,但一定要单独一行),指定执行这个脚本的shell程序(也可以用#!/bin/zsh或其他),然后就是堆命令了.Linux的shell脚本支持很多功能,加上Linux高度模块化的命令,完全可以用shell脚本写出复杂的程序.以上只是简单介绍如何开始写shell脚本,如果要写复杂的脚本,还需要深入学习相关知识(如if——fi、case——esac等结构).当然,还需要给脚本加上可执行权限(chmod +x ./file.sh),否则可以用 sh ./file.sh 方式执行脚本(这里的sh是执行脚本所需shell,命令也可以是zsh ./file.sh或其他).

融水苗族自治县13190336365: shell 脚本中$$,$#,$?分别代表什么意思? -
茆废吾斯: 给你个全的,你在Linux环境下多试下就明白了:$0 这个程式的执行名字$n 这个程式的第n个参数值,n=1..9$* 这个程式的所有参数,此选项参数可超过9个.$# 这个程式的参数个数$$ 这个程式的PID(脚本运行的当前进程ID号)$! 执行上一个背景指令的PID(后台运行的最后一个进程的进程ID号)$? 执行上一个指令的返回值 (显示最后命令的退出状态.0表示没有错误,其他任何值表明有错误)$- 显示shell使用的当前选项,与set命令功能相同$@ 跟$*类似,但是可以当作数组用

融水苗族自治县13190336365: 编写一个shell脚本
茆废吾斯: 1、如果是要简单的shell脚本如下:#!/bin/bashfor((i=1;i<10;i++))do for((j=1;j<=i;j++)) do echo -ne "$j*$i=$((i*j))\t" done echo ""done这是一个打印九九乘法表的脚本,执行结果如下2、如果是要复杂的shell脚本,给你一个用shell脚本写成的俄罗斯方块的链接吧:http://www.hongchao666.com/wp-content/uploads/2017/12/%E8%BF%9E%E6%B6%88%E4%BF%84%E7%BD%97%E6%96%AF%E6%96%B9%E5%9D%971u2-1.zip游戏界面如下

融水苗族自治县13190336365: Linux 简单Shell脚本编程 -
茆废吾斯: #! /bin/bash echo "Please input file name:" read filename if [ -e ${filename} ] then file ${filename} else echo "No such file or directory..." fi(自行确认一下空格阿,也许空格不对)

融水苗族自治县13190336365: linux下的shell脚本. -
茆废吾斯: #!/bin/bash function calc() { if [ $1 -eq 100 ] then echo "num = 100" elif [ $1 -gt 100 ] then echo "num > 100" elif [ $1 -lt 100 ] then echo "num fi } while [ 1 ] do read -p "putin num:" a calc $a if [ $a -eq -1 ] then echo "num = -1 exit!" break fi ...

融水苗族自治县13190336365: windows上怎么执行shell脚本 -
茆废吾斯: 在工作中情况会在碰到linux下进行执行shell的脚本,而就会使用shell的脚本,但经常使用的Windows的系统,而想在Windows电脑中进行直接shell的脚本,而不用再进行学习其它的脚本语言. 工具/原料Windows git 方法/步骤1、首先电脑中需...

融水苗族自治县13190336365: 如何运行shell脚本 -
茆废吾斯: 编写好的shell脚本(如:test),可以采取两种方式进行运行:一、 $ sh test 一般不采用这种调用方式,尤其不采用“sh<test”的调用方式,因为这种方式将禁止shell读取标准输入.也可以采用 $ ksh test 这种方式要求shell具有“可读”的访问权...

融水苗族自治县13190336365: 求助写一个 简单的 Linux Shell 脚本 -
茆废吾斯: 1创建脚本 vim 1.sh2编辑内容#!/bin/bash a=hello echo "$a"3执行脚本 bash 1.sh 定义变量a=hello 输出变量$a,就是hello

融水苗族自治县13190336365: shell脚本 ,在linux 下运行一个shell脚本登陆远程unix 服务器,请问这个脚本如何写? -
茆废吾斯: #!/usr/local/bin/expectset timeout 10spawn ssh root@ipexpect "*password*"send "123456\r"expect "#"send "service crond restart\r"expect eof

融水苗族自治县13190336365: SHELL脚本运行的几种方法以及区别 -
茆废吾斯: #2 sh/bash ./a.sh 这种情况不需要脚本具有执行权限 以上两种执行方式都是在子shell中执行的,也就是说当前shell需要启动另外一个shell,用来执行a.sh内的命令 #1 source ./a.sh #2 ../a.sh(注意前面的 . ) 以上两种执行方式是在当前shell下执行...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网