显示shell脚本的echo内部的参数

时间:2017-07-22 11:50:51

标签: bash shell

我正在尝试在shell脚本函数中运行以下匿名块时生成文件。我只复制了那部分内容。 代码是。

echo " sqlplus -s $user_name/$password << EOF"
echo "set serveroutput on"
echo "set echo on"
echo "WHENEVER SQLERROR EXIT SQL.SQLCODE"
echo "declare"
echo "        l_workspace_id number;"
echo "        l_workspace_name varchar2(100) := '$workspace_name';"
echo "        l_application_id number := $application_id;"
echo "        l_parsing_schema varchar2(100) := '$user_name';"
echo "begin"
echo "    select workspace_id into l_workspace_id"
echo "      from apex_workspaces"
echo "where upper(workspace) = upper(l_workspace_name);"
echo ""
echo "    apex_application_install.set_workspace_id( l_workspace_id );"
echo "    apex_application_install.set_application_id(l_application_id);"
echo "    apex_application_install.generate_offset;"
echo "    apex_application_install.set_schema( l_parsing_schema );"
echo "    apex_application_install.set_application_alias( 'F' ||    apex_application_install.get_application_id );"
echo "end;"
echo "/"
echo "@f$APPLICATIONID.sql"
echo "commit;"
echo "exit;"
echo "EOF"

然后在另一个shell脚本文件中打印此echo。问题是当我检查生成的.sh文件时,我得到以下输出

sqlplus -s / << EOF
set serveroutput on
set echo on
WHENEVER SQLERROR EXIT SQL.SQLCODE
declare
    l_workspace_id number;
    l_workspace_name varchar2(100) := ;
    l_application_id number := ;
    l_parsing_schema varchar2(100) := ;

我保留$的所有地方都是空白的。有人可以告诉我哪里出错了吗?

感谢。

1 个答案:

答案 0 :(得分:3)

当你echo这些行时,变量会扩展到它们的值。 但那不是你想做的。 您希望稍后扩展变量, 运行生成的脚本时。 要做到这一点,你需要:

  • 使用$
  • 逃离所有\$
  • 将所有echo "..."更改为echo '...',将双引号替换为单引号,以便$不会展开

第三个更好的选择是使用带有"..."所附标签的here-document,如下所示:

cat << "OUTER"
sqlplus -s $user_name/$password << EOF
set serveroutput on
set echo on
WHENEVER SQLERROR EXIT SQL.SQLCODE
declare
        l_workspace_id number;
        l_workspace_name varchar2(100) := '$workspace_name';
        l_application_id number := $application_id;
        l_parsing_schema varchar2(100) := '$user_name';
begin
    select workspace_id into l_workspace_id
      from apex_workspaces
where upper(workspace) = upper(l_workspace_name);

    apex_application_install.set_workspace_id( l_workspace_id );
    apex_application_install.set_application_id(l_application_id);
    apex_application_install.generate_offset;
    apex_application_install.set_schema( l_parsing_schema );
    apex_application_install.set_application_alias( 'F' ||    apex_application_install.get_application_id );
end;
/
@f$APPLICATIONID.sql
commit;
exit;
EOF
OUTER