在编写shell程序时,我们经常使用/bin/sh和/bin/bash.我经常使用bash,但我不知道它们之间有什么区别.
bash和之间的主要区别是sh什么?
我们究竟需要意识到在编程时bash和sh?
我正在Mac上制作NW.js应用程序,并希望通过双击图标以开发模式运行应用程序.第一步,我正在尝试使我的shell脚本工作.
在Windows上使用VSCode(我想获得时间),我run-nw在项目的根目录创建了一个文件,其中包含:
#!/bin/bash
cd "src"
npm install
cd ..
./tools/nwjs-sdk-v0.17.3-osx-x64/nwjs.app/Contents/MacOS/nwjs "src" &
Run Code Online (Sandbox Code Playgroud)
但我得到这个输出:
$ sh ./run-nw
: command not found
: No such file or directory
: command not found
: No such file or directory
Usage: npm <command>
where <command> is one of: (snip commands list)
(snip npm help)
npm@3.10.3 /usr/local/lib/node_modules/npm
: command not found
: No such file or directory
: command not found
Run Code Online (Sandbox Code Playgroud)
我真的不明白:
\r\n用\n(以防\r造成了问题),但它改变不了什么.dirname指令),或者它可能不知道cd …考虑这个简单的shell脚本:
#!/bin/sh
fruitlist="Apple Pear Tomato Peach Grape"
for fruit in $fruitlist
do
if [ "$fruit" = "Tomato" ] || [ "$fruit" = "Peach" ]
then
echo "I like ${fruit}es"
else
echo "I like ${fruit}s"
fi
done
Run Code Online (Sandbox Code Playgroud)
当我将它粘贴到cygwin窗口时,它工作正常但是当我将其保存为文本文件test.sh并从cygwin终端运行时我得到了这个:
$ ./test.sh
./test.sh: line 4: syntax error near unexpected token `$'do\r''
'/test.sh: line 4: `do
Run Code Online (Sandbox Code Playgroud)
但是,如果我删除换行符,它会起作用:
#!/bin/sh
fruitlist="Apple Pear Tomato Peach Grape"
for fruit in $fruitlist
do if [ "$fruit" = "Tomato" ] || [ "$fruit" = "Peach" ]
then echo "I like …Run Code Online (Sandbox Code Playgroud) 我有以下脚本:
#!/bin/sh
#parameters definition
ROOT_PATH=/var/www/
msql_psw="cc"
mysql_login="bb"
mysql_db_name="aa"
echo START MYSQL DUMP
NOW=$(date +"%Y-%m-%d")
SQLFILE="log-$NOW.sql"
mysqldump --add-drop-table -u "$mysql_login" --password="$mysql_psw" "$mysql_db_name" > "$ROOT_PATH/$SQLFILE"
Run Code Online (Sandbox Code Playgroud)
当它与 一起运行时sh -x,它会给出空行的命令未找到错误,甚至非空行也无法正常工作:
sh -x export_db.sh
+ $'\r'
: command not found2:
+ ROOT_PATH=$'/var/www/\r'
+ msql_psw=$'cc\r'
+ mysql_login=$'bb\r'
+ mysql_db_name=$'aa\r'
+ $'\r'
: command not found8:
+ echo START MYSQL $'DUMP\r'
START MYSQL DUMP
+ $'\r'
: command not found10:
++ date +%Y-%m-%d
+ NOW=$'2014-10-02\r'
+ SQLFILE=$'log-2014-10-02\r.sql\r'
+ mysqldump --add-drop-table -u $'bb\r' --password= $'aa\r'
: …Run Code Online (Sandbox Code Playgroud)