运行 shell 脚本时 sh -x 与 bash -x

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11943985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:01:18  来源:igfitidea点击:

sh -x vs bash -x while running shell script

linuxbashshellunixsunos

提问by AKIWEB

Below is my shell script which I am using to create a PIE Graph.

下面是我用来创建 PIE Graph 的 shell 脚本。

#! /bin/bash

TEMP=$(mktemp -t chart.html)
QUERY1=36
QUERY2=64
cat > $TEMP <<EOF
<html>
  <head>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">

      // Load the Visualization API and the piechart package.
      google.load('visualization', '1.0', {'packages':['corechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(drawChart);

      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {

        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Title');
        data.addColumn('number', 'Value');
        data.addRows([
          ['Error Percentage', $QUERY1],
          ['No Error Percentage', $QUERY2]
        ]);

        // Set chart options
        var options = {'title':'Errors',
                       'width':400,
                       'height':300};

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>

  <body>
    <!--Div that will hold the pie chart-->
    <div id="chart_div"></div>
  </body>
</html>
EOF

# open browser
case $(uname) in
   Darwin)
      open -a /Applications/Google\ Chrome.app $TEMP
      ;;

   Linux|SunOS)
      firefox $TEMP
      ;;
 esac

Problem Statement:-

问题陈述:-

I saved the above file as chart.sh. And whenever I am trying to run the above chart.shfile as sh -x chart.sh

我将上述文件另存为chart.sh. 每当我尝试运行上述chart.sh文件时sh -x chart.sh

I always get the error as-

我总是收到错误 -

syntax error at line number 3: `TEMP=$' unexpected

But when I try to run the above shfile as-

但是当我尝试将上述sh文件作为-

bash -x chart.sh

then I don't get any error. Why is it so? Is there anything wrong in my shell script and suppose If I need to run it like sh -x chart.shalways then what I need to make changes in my shell script?

那我什么都没有error。为什么会这样?我的 shell 脚本有什么问题,假设如果我需要像sh -x chart.sh往常一样运行它,那么我需要在我的 shell 脚本中进行哪些更改?

I am running SunOS.

我在跑步SunOS

回答by rid

shand bashcan point to completely different executables. The fact that shis a symbolic link to bashon certain systems should not make you assume the same is true everywhere. If you need bash, use it explicitly.

sh并且bash可以指向完全不同的可执行文件。在某些系统上sh是符号链接的事实bash不应该让您假设在任何地方都是如此。如果需要bash,请明确使用它。

EDIT(mpapis): Also note that even when you call bashvia shit does not behave exactly the same.

编辑(mpapis):另请注意,即使您bash通过sh它调用,它的行为也不完全相同。

回答by Random832

See this page: http://docs.oracle.com/cd/E19082-01/819-2252/6n4i8rtus/index.htmlfor information on how to set your PATHvariable to call the POSIX-compatible version of shand other tools.

请参阅此页面:http: //docs.oracle.com/cd/E19082-01/819-2252/6n4i8rtus/index.html,了解有关如何设置PATH变量以调用 POSIX 兼容版本sh和其他工具的信息。