Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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
Linux 未通过bash中的shell脚本执行curl命令_Linux_Bash_Shell_Curl_Gnome Terminal - Fatal编程技术网

Linux 未通过bash中的shell脚本执行curl命令

Linux 未通过bash中的shell脚本执行curl命令,linux,bash,shell,curl,gnome-terminal,Linux,Bash,Shell,Curl,Gnome Terminal,我正在学习shell脚本!出于同样的原因,我尝试在ubuntu终端上使用curl下载facebook页面 t、 sh含量 vi@vi-Dell-7537(Desktop) $ cat t.sh curlCmd="curl \"https://www.facebook.com/vivekkumar27june88\"" echo $curlCmd ($curlCmd) > ~/Desktop/fb.html 作为运行脚本时出错 vi@vi-Dell-7537(Desktop) $ ./t

我正在学习shell脚本!出于同样的原因,我尝试在ubuntu终端上使用curl下载facebook页面

t、 sh含量

vi@vi-Dell-7537(Desktop) $ cat t.sh 
curlCmd="curl \"https://www.facebook.com/vivekkumar27june88\""
echo $curlCmd
($curlCmd) > ~/Desktop/fb.html
作为运行脚本时出错

vi@vi-Dell-7537(Desktop) $ ./t.sh 
curl "https://www.facebook.com/vivekkumar27june88"
curl: (1) Protocol "https not supported or disabled in libcurl
但是,如果直接运行命令,那么它工作正常

vi@vi-Dell-7537(Desktop) $ curl "https://www.facebook.com/vivekkumar27june88"
<!DOCTYPE html>
<html lang="hi" id="facebook" class="no_js">
<head><meta chars.....
vi@vi-Dell-7537(台式机)$curl“https://www.facebook.com/vivekkumar27june88"

仅将脚本
t.sh
创建为这一行:

curl -k "https://www.facebook.com/vivekkumar27june88" -o ~/Desktop/fb.html
根据
手册卷曲

-k,--unsecure

(SSL) This option explicitly allows curl to perform "insecure" SSL connections transfers.  
All  SSL  connections  are  attempted  to be made secure by using the CA certificate bundle
installed by default. This makes all connections considered "insecure" fail unless -k,
--insecure is used.
-o文件

Store output in the given filename.

嵌入在括号中的命令作为子shell运行,因此环境变量将丢失

尝试评估:

curlCmd="curl 'https://www.facebook.com/vivekkumar27june88' > ~/Desktop/fb.html"
eval $curlCmd
正如@Chepner所说的,去读吧。总而言之,你应该如何做这类事情取决于你的目标是什么

  • 如果您不需要存储命令,请不要!存储命令很难做到正确,因此,如果不需要,只需跳过这些混乱,直接执行即可:

    curl "https://www.facebook.com/vivekkumar27june88" > ~/Desktop/fb.html
    
  • 如果要隐藏命令的详细信息,或要大量使用该命令,而不希望每次都将其写出,请使用以下函数:

    curlCmd() {
        curl "https://www.facebook.com/vivekkumar27june88"
    }
    
    curlCmd > ~/Desktop/fb.html
    
  • 如果需要逐段构建命令,请使用数组而不是普通字符串变量:

    curlCmd=(curl "https://www.facebook.com/vivekkumar27june88")
    for header in "${extraHeaders[@]}"; do
        curlCmd+=(-H "$header")   # Add header options to the command
    done
    if [[ "$useSilentMode" = true ]]; then
        curlCmd+=(-s)
    fi
    
    "${curlCmd[@]}" > ~/Desktop/fb.html    # This is the standard idiom to expand an array
    
  • 如果要打印命令,最好的方法通常是使用
    set-x

    集合x curl”“>~/Desktop/fb.html 集合+x

    …但如果需要,也可以使用阵列方法执行类似操作:

    printf "%q " "${curlCmd[@]}"    # Print the array, quoting as needed
    printf "\n"
    "${curlCmd[@]}" > ~/Desktop/fb.html
    

在ubuntu 14.04中安装以下软件

  • sudo apt get安装php5 curl
  • sudo-apt-get-install-curl
  • 然后运行sudo服务apache2重启 使用curl“curl support:enabled”检查phpinfo()是否启用

    然后检查shell脚本中的命令

    结果=
    curl-L“http://sitename.com/dashboard/?show=api&action=queue_proc&key=$JOBID“2>/dev/null

    echo$结果

    你会得到回应


    谢谢。

    你好,阿努巴瓦!即使在向curl添加-k标志后也会出现相同的错误。通过运行
    type curl
    来检查shell中curl的路径,并在脚本中进行相同的检查。当运行
    $type curl
    时,获取以下输出
    curl是散列的(/usr/bin/curl)
    。但从脚本输出是
    curl是/usr/bin/curl
    。你能告诉我散列在这里是什么意思吗?嗯,两个似乎运行相同的
    curl
    binary。不确定shell与脚本有什么不同。所以复制/粘贴
    curl-k“https://www.facebook.com/vivekkumar27june88“-o~/Desktop/fb.html
    工作正常,但
    bash./t.sh
    给出了该错误?但我不明白为什么脚本会产生这种差异。你能解释一下吗…谢谢:)不$cmd在我的ubuntu 14.04上不起作用<代码>vi@vi-Dell-7537(台式机)$/t.sh curl-k“https://www.facebook.com/vivekkumar27june88libcurl中不支持或禁用“curl:(1)协议”httpsvi@vi-Dell-7537(台式机)$cat t.sh#!/bin/sh curlCmd=“curl-k”https://www.facebook.com/vivekkumar27june88\"“echo$curlCmd$curlCmd>~/Desktop/fb.html
    在这种情况下,需要
    eval
    来解释命令字符串中的单引号。但是
    eval
    为粗心的人提供了大量陷阱,你最好避开它们。有更好的方法可以做到这一点,比如将命令存储为数组(或者根本不存储它!)。