Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 将从shell脚本接收的参数传递到同一shell脚本内的db2查询_Bash_Shell_Db2_Db2 Luw - Fatal编程技术网

Bash 将从shell脚本接收的参数传递到同一shell脚本内的db2查询

Bash 将从shell脚本接收的参数传递到同一shell脚本内的db2查询,bash,shell,db2,db2-luw,Bash,Shell,Db2,Db2 Luw,我正在编写一个shell脚本,它采用1个参数表名。在同一个shell脚本中,我有一个db2查询——“Select*fromtable_name”。我想为从shell脚本参数接收到的db2查询提供一个表名。我如何做到这一点 我的剧本到目前为止- echo "Content Display" while [ -n "$1" ]; do case "$1" in -f) echo "File mode has selected" ;;

我正在编写一个shell脚本,它采用1个参数表名。在同一个shell脚本中,我有一个db2查询——“Select*fromtable_name”。我想为从shell脚本参数接收到的db2查询提供一个表名。我如何做到这一点

我的剧本到目前为止-

    echo "Content Display"
        while [ -n "$1" ]; do
          case "$1" in
          -f) echo "File mode has selected" ;;
          --)
          shift
          break
          ;;
    esac
    shift
    done
    param = "$1"
    db2 "select * from 'param'"

要在双引号字符串中展开变量,只需在变量名称前面加一个
$

param="world"
echo "Hello $param"
就你而言:

param="$1"
db2 "select * from $param"
如果需要消除歧义,可以使用
${}

param="29"
echo "It is $paramC outside"  # Wrong, accesses $paramC
echo "It is ${param}C outside # Right, accesses $param and appends the letter C

下面是一个基于您的需求的示例shell脚本。它在AIX和Linux上进行了验证

  • 将下面另存为prep.sh,运行“chmod 777 prep.sh”并执行它
  • 将下面另存为get.sh并运行“chmod 777 get.sh”
  • 执行get.sh,如下所示:
  • 格特酒店

    注意: 它连接数据库db1,选择hotel表,然后返回三行,如下所示:

    C1
    --------------------
    Nice hotel
    Good hotel
    Fancy hotel
    
      3 record(s) selected.
    

    希望这能有所帮助。

    你能把你的脚本发布到目前为止,给人们一个起点,看看你到底想做什么吗?如果没有这一点,人们可以做的最有用的事情可能就是将您链接到文档。对不起,我的错误,更新了帖子。这是您的问题“如何在bash中使用字符串中的变量?”我正在将从参数接收的值存储到名为param的变量中。我想在db2查询中使用这个变量。所以基本上用户将提供一个表名,我想通过对其运行db2 select查询来返回表的内容。我想您的意思是
    db2“select*from${param}”
    而不是
    db2“select*from'param'
    #!/bin/sh
    
    if [ -z "$1" ] ; then
      echo "no table name"
      exit
    else
      tabname=$1
    fi
    
    db2 -v "connect to db1"
    db2 -v "select * from $tabname"
    db2 -v "terminate"
    
    C1
    --------------------
    Nice hotel
    Good hotel
    Fancy hotel
    
      3 record(s) selected.