Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.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
shell脚本中的Read命令_Shell - Fatal编程技术网

shell脚本中的Read命令

shell脚本中的Read命令,shell,Shell,我有这个剧本: echo -e ">> Please, insert edition folder name: "; read editionfolder; RSeditionfolder="$editionfolder"; sh -c $POSTGRESbin'psql -U '$POSTGRESuser' -h localhost -d '$POSTGRESdb' -c "select count(*) from d3_folders wh

我有这个剧本:

    echo -e ">> Please, insert edition folder name: ";
    read editionfolder;
    RSeditionfolder="$editionfolder";    
    sh -c $POSTGRESbin'psql -U '$POSTGRESuser' -h localhost -d '$POSTGRESdb' -c "select count(*) from d3_folders where folder_name = '\'$RSeditionfolder\'';"' > $queryFolder$tmpFile
例如,如果我在editionfolder var:CD_199 FOTOS中插入,我会收到一个错误:

FOTOS';": -c: line 0: unexpected EOF while looking for matching `"'
FOTOS';": -c: line 1: syntax error: unexpected end of file
$queryFolder具有“query/”值,$tmpFile具有“tmpFile”

但我不知道我做错了什么

此查询的结果是:

 count 
-------
     0
(1 row)
在这种情况下,我只想要数字0。我正在尝试将结果放入一个文件中,下一步是在这个文件中使用sed,我知道另一种方式,如果我知道,我将非常感激


谢谢

省略
sh-c…
中的所有单引号,并用双引号引用整个命令:

sh -c "$POSTGRESbin/psql -U $POSTGRESus -h ..."

双引号确保只有一个参数,同时允许替换shell变量。

给定的命令有点混乱,我会这样写

sh -c "${POSTGRESbin}psql -U $POSTGRESuser -h localhost -d $POSTGRESdb -c \"select count(*) from d3_folders where folder_name = '$RSeditionfolder';\"" > ${queryFolder}${tmpFile}

首先,我将使用双引号中的$变量去掉所有单引号。其次,最好使用显式变量名边框,
${queryFolder}${tmpFile}
来澄清子句“$queryFolder$tmpFile”。但是总的来说,脚本是好的。

POSTGRESbin是一个变量的单一名称还是它是${POSTGRES}bin的串联?另外,我想在这里交换转义:
\'$RSeditionfolder\'
\'$RSeditionfolder'\
执行类似于
sh-c“${POSTGRESbin}psql-U$POSTGRESuser-h localhost-d$POSTGRESdb-c\”的操作(*)来自d3_文件夹,其中文件夹_名称=“$RSeditionfolder”\“>${queryFolder}${tmpFile}
做你需要做的事?@EarlGray I love you:)工作完美!!!谢谢