Step by step how to write Shell scripts

  • 2020-05-30 21:57:04
  • OfStack


Request api online to get the return results in json format Parse 1 value in json Store the value in a file

The development of preparation

1, shell

Quick start:

cd ~
mkdir shell_tut
cd shell_tut
for ((i=0; i<10; i++)); do
 touch test_$i.txt

Create a folder and create 10 new files in it

2, curl

Use curl to request the network and get the return value

Basic usage of curl:

$ curl

Return directly to the web content

$ curl -o [ The file name ]

Storing web content in a file is the equivalent of the wget command

Assignment operation:

echo $response

Note: = no space before or after


jq is a great tool for command-line parsing of json

The installation

Directly download Linux64 version from the official website (I downloaded it locally and then uploaded it to the server. Before, I directly used wget to download 1 from the server and report the error Segmentation fault.

Put it copy into the bin directory

cp jq-linux64 /usr/bin/jq


Then you can happily use the jq command:

You can directly check the official website Tutorial

curl ''

It returns a list of json arrays

curl '' | jq '.'

Use. To point directly to the root node of json

curl '' | jq '.[0]'

[0] is the object that takes the 0th bit of the array

jq '.[0] | {message: .commit.message, name:}'

The command above will generate 1 {message: "..." , name: "..." } json object.

shell file read and write

1, write,

Method 1:

Write the contents of ll to the fileName file

> Is covered, > > I'm going to append it

ll >fileName

However, when writing script, you need to add echo

$ curl

2, read,

$ curl

fileName is the file name to read


Related articles: