相关疑难解决方法(0)

使用bash/shell脚本打开并将数据写入文本文件

如何通过Linux中的shell脚本自动将数据写入文本文件?

我能够打开文件.但是,我不知道如何向它写入数据.

bash shell

207
推荐指数
9
解决办法
61万
查看次数

使用curl POST和bash脚本函数中定义的变量

当我回音时,我得到了这个,当我将它输入终端时运行

curl -i \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST --data '{"account":{"email":"akdgdtk@test.com","screenName":"akdgdtk","type":"NIKE","passwordSettings":{"password":"Starwars1","passwordConfirm":"Starwars1"}},"firstName":"Test","lastName":"User","middleName":"ObiWan","locale":"en_US","registrationSiteId":"520","receiveEmail":"false","dateOfBirth":"1984-12-25","mobileNumber":"9175555555","gender":"male","fuelActivationDate":"2010-10-22","postalCode":"10022","country":"US","city":"Beverton","state":"OR","bio":"This is a test user","jpFirstNameKana":"unsure","jpLastNameKana":"ofthis","height":"80","weight":"175","distanceUnit":"MILES","weightUnit":"POUNDS","heightUnit":"FT/INCHES"}' https://xxx:xxxxx@xxxx-www.xxxxx.com/xxxxx/xxxx/xxxx
Run Code Online (Sandbox Code Playgroud)

但是当在bash脚本文件中运行时,我收到此错误

curl: (6) Could not resolve host: application; nodename nor servname provided, or not known
curl: (6) Could not resolve host: is; nodename nor servname provided, or not known
curl: (6) Could not resolve host: a; nodename nor servname provided, or not known
curl: (6) Could not resolve host: test; nodename nor servname provided, or not known
curl: (3) [globbing] …
Run Code Online (Sandbox Code Playgroud)

bash json curl javascript-objects

144
推荐指数
8
解决办法
20万
查看次数

如何cat << EOF >>包含代码的文件?

我想使用cat <<EOF >>以下代码将代码打印到文件中:

cat <<EOF >> brightup.sh
!/bin/bash
curr=`cat /sys/class/backlight/intel_backlight/actual_brightness`
if [ $curr -lt 4477 ]; then
   curr=$((curr+406));
   echo $curr  > /sys/class/backlight/intel_backlight/brightness;
fi
EOF
Run Code Online (Sandbox Code Playgroud)

但是当我检查文件输出时,我得到了这个:

!/bin/bash
curr=1634
if [  -lt 4477 ]; then
   curr=406;
   echo   > /sys/class/backlight/intel_backlight/brightness;
fi
Run Code Online (Sandbox Code Playgroud)

我尝试使用单引号,但输出也带有单引号.我该如何避免这个问题?

unix linux heredoc sh

79
推荐指数
4
解决办法
15万
查看次数

使用Bash将大量文本回显到文件

我需要运行一个Bash脚本,它可以将300行Groovy脚本回显到tmp文件.最好的方法是什么?

我目前的解决方法是将脚本文件放在网上并下载.

bash

27
推荐指数
3
解决办法
4万
查看次数

用于Windows的Git中的Bash:使用带有args的CMD.exe/C运行命令时的怪异

这更令人烦恼而不是问题,但我非常想了解这里的语义.

我想要做的就是在临时命令提示会话上运行任意命令,该会话本身在bash会话下运行.

我的成功率是50/50,因为一些命令按预期工作,而其他命令则没有那么多.

我认为问题可能在于没有正确排列的参数(即缺失或合并的参数)

我会尽力解释我的意思是怪异的一系列命令和响应.(我正试着在屏幕上打印测试字.)

我在GNU bash下运行这些,版本3.1.0(1)-release(i686-pc-msys)与Git-1.8.4捆绑在一起:

第一次尝试:

$ cmd /c echo test
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
c:\>
Run Code Online (Sandbox Code Playgroud)

第二次尝试:

$ cmd '/c echo test'
test"
Run Code Online (Sandbox Code Playgroud)

第三次尝试:

$ cmd "/c echo test"
test"
Run Code Online (Sandbox Code Playgroud)

第四次尝试:

$ cmd /c\ echo\ test
test"
Run Code Online (Sandbox Code Playgroud)

第五次尝试:

$ cmd "/c echo" test
'echo" test' is not recognized as an internal or external command,
operable program or batch file.
Run Code Online (Sandbox Code Playgroud)

我真的很感激上述行为的任何指示或见解,因为这对我来说是不合理的,让我疯狂! …

windows git bash cmd git-bash

24
推荐指数
2
解决办法
8349
查看次数

我需要cat将heredoc写入文件吗?

我有一个写入文件的脚本,如下所示:

cat >myfile <<EOF
some lines
and more lines
EOF
Run Code Online (Sandbox Code Playgroud)

但我不确定这是否是无用的猫......

bash cat

8
推荐指数
1
解决办法
2867
查看次数

这个错误是什么意思?(SC2129:考虑使用{cmd1; cmd2;} >>文件而不是单独的重定向.)

我正在编写一个脚本来为我的博客生成草稿.运行ShellCheck后,我一直看到弹出这个错误.这是什么意思,有人可以提供一个例子吗?

SC2129: Consider using { cmd1; cmd2; } >> file instead of individual redirects.

另外,我不确定我需要做什么才能将值传递$title"Title"帖子的YAML中的字段......

#!/bin/bash

# Set some variables

var site_path=~/Documents/Blog
drafts_path=~/Documents/Blog/_drafts
title="$title"

# Create the filename

title=$("$title" | "awk {print tolower($0)}")
filename="$title.markdown"
file_path="$drafts_path/$filename"
echo "File path: $file_path"

# Create the file, Add metadata fields

echo "---" > "$file_path"
{
    echo "title: \"$title\""
}   >> "$file_path"

echo "layout: post" >> "$file_path"
echo "tags: " >> "$file_path"
echo "---" >> "$file_path"

# Open the …
Run Code Online (Sandbox Code Playgroud)

bash shell shellcheck

7
推荐指数
1
解决办法
1055
查看次数

如何将星号传递到bash heredoc?

我在bash shell中执行带星号的MySQL语句:

query=`cat <<EndOfMySQL
INSERT tmp_table
SELECT * FROM table
;
EndOfMySQL
`

echo $query

echo $query | mysql database
Run Code Online (Sandbox Code Playgroud)

问题是星号被当前目录中的文件列表替换,并且查询变得错误。如何避免这种行为?我使用反引号还是都没关系$()。我希望有一些转义序列,例如\*,但至少这是行不通的。

mysql bash glob heredoc

4
推荐指数
1
解决办法
1762
查看次数

如何对字符串中的所有行共同使用bash"吃"缩进字符?

我在shell变量中有一些多行字符串.字符串的所有行都有一个未知的缩进级别,至少有几个空格字符(在我的示例中为8个空格,但可以是任意的).我们来看一下这个示例字符串:

        I am at the root indentation level (8 spaces).
        I am at the root indentation level, too.
            I am one level deeper
            Am too
        I am at the root again
                I am even two levels deeper
                    three
                two
            one
        common
        common
Run Code Online (Sandbox Code Playgroud)

我想要的是一个Bash函数或命令去掉常见的缩进级别(这里有8个空格),所以我得到了这个:

I am at the root indentation level (8 spaces).
I am at the root indentation level, too.
    I am one level deeper
    Am too
I am at the root again
        I am even two levels deeper
            three …
Run Code Online (Sandbox Code Playgroud)

string bash indentation line-by-line

2
推荐指数
1
解决办法
135
查看次数