Step by step how to write Shell scripts
- 2020-05-30 21:57:04
- OfStack
Question:
The development of preparation
1, shell
Quick start:
#!/bin/sh
cd ~
mkdir shell_tut
cd shell_tut
for ((i=0; i<10; i++)); do
touch test_$i.txt
done
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 www.sina.com
Return directly to the web content
$ curl -o [ The file name ] www.sina.com
Storing web content in a file is the equivalent of the wget command
Assignment operation:
response=$(curl www.sina.com)
echo $response
Note: = no space before or after
jq
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
use
Then you can happily use the jq command:
You can directly check the official website Tutorial
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5'
It returns a list of json arrays
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.'
Use. To point directly to the root node of json
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0]'
[0] is the object that takes the 0th bit of the array
jq '.[0] | {message: .commit.message, name: .commit.committer.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 a.sh script, you need to add echo
$ curl www.sina.com
0
2, read,
$ curl www.sina.com
1
fileName is the file name to read
conclusion