Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions ebook/en/content/046-the-curl-command.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
# The `curl` command

In linux, `curl` is a tool to transfer data from or to a server, using one of the supported protocols(DICT, FILE ,FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP).
In Linux, curl is a powerful command-line tool used to transfer data from or to a server using a wide variety of protocols, including HTTP, HTTPS, and FTP. It is often used for testing APIs, downloading files, and automating web-related tasks.

## Example :
## The syntax of the `curl` command :

```bash
$ curl example.com
$ curl [options...] <url>
```

The command will print the source code of the example.com homepage in the terminal window.

## The syntax of the `curl` command is :
## Common Options :

curl has over 200 options! Here are some of the most common and useful ones.

| Option | Long Version | Description |
|----------------------|---------------------|------------------------------------------------------------------------|
| `-O` | `--remote-name` | Downloads the file and saves it with the same name as the remote file. |
| `-o <file>` | `--output <file>` | Saves the downloaded output to a specific filename. |
| `-L` | `--location` | Follows redirects if the server reports that the requested page has moved. |
| `-X <METHOD>` | `--request <METHOD>`| Specifies the HTTP request method to use (e.g., POST, PUT, DELETE). |
| `-H <header>` | `--header <header>` | Allows you to add a custom HTTP header to your request. |

```bash
$ curl [options...] <url>
```

## Options :
## Examples :

Options start with one or two dashes. Many of the options require an additional value next to them.
### 1. View the source code of a webpage

The short "single-dash" form of the options, `-d` for example, may be used with or without a space between it and its value, although a space is a recommended separator. The long "double-dash" form, `-d`, `--data` for example, requires a space between it and its value.
This is the simplest use of curl. It will fetch the content from the URL and print its HTML source code directly to your terminal.
```bash
$ curl example.com
```
### 2. Download a file

The -O flag is used to download a file. curl will save it in your current directory using the same name as the remote file.

Short version options that don't need any additional values can be used immediately next to each other, like for example you can specify all the options `-O`, `-L` and `-v` at once as `-OLv`.
```bash
$ curl -O https://github.com/bobbyiliev/101-linux-commands/archive/refs/tags/v1.0.zip
```
### 3. Download a file and rename it

In general, all boolean options are enabled with `--option` and yet again disabled with `--no-option`. That is, you use the exact same option name but prefix it with `no-`. However, in this list we mostly only list and show the `--option` version of them. (This concept with `--no` options was added in 7.19.0. Previously most options were toggled on/off through repeated use of the same command line option.)
Using the -o flag, you can specify a new name for the downloaded file.
```bash
$ curl -o linux-commands.zip https://github.com/bobbyiliev/101-linux-commands/archive/refs/tags/v1.0.zip
```

## Installation:

Expand Down
71 changes: 60 additions & 11 deletions ebook/en/content/121-the-wait-command.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,76 @@
# The `Wait` commands

It is a command that waits for completing any running process of given id. if the process id is not given then it waits for all current child processes to complete.
The wait command is a shell builtin that pauses script execution until a specific background process, or all running child processes, have finished.

## Example
Its primary purpose is to synchronize tasks, ensuring that a script doesn't continue to the next step until prerequisite background jobs are complete. A background process is a command that is run with an ampersand (&) at the end, which tells the shell to run it without waiting for it to finish.

This example shows how the `wait` command works : <br />
## Syntax

**Step-1**:
```bash
$ wait [PID]
```
[PID] - An optional Process ID to wait for. If no PID is given, wait will wait for all active child processes to complete.

Create a file named "wait_example.sh" and add the following script to it.
## Examples

```
### 1. Waiting for a Specific Process
This example shows how to launch a single background process and wait for it specifically.

### Script:
```bash
#!/bin/bash
echo "Wait command" &
echo "This process will run in the background..." &
process_id=$!

echo "Script is now waiting for process ID: $process_id"
wait $process_id
echo "Exited with status $?"
echo "Process $process_id has finished."
echo "The script exited with status: $?"
```
### Explanation:

**Step-2**:
* &: The ampersand runs the echo command in the background, allowing the script to immediately continue to the next line.

Run the file with bash command.
* $!: This is a special shell variable that holds the Process ID (PID) of the most recently executed background command. We save it to the process_id variable.

```
* wait $process_id: This is the key command. The script pauses here until the process with that specific ID is complete.

* $?: This variable holds the exit status of the last command that finished. An exit status of 0 means success.


### Output:

```bash
$ bash wait_example.sh
Script is now waiting for process ID: 12345
This process will run in the background...
Process 12345 has finished.
The script exited with status: 0
```

### 2. Waiting for All Background Processes
This is the most common use case. Here, we launch several background tasks and then use a single wait command to pause until all of them are done.

### Script:
```bash
#!/bin/bash

echo "Starting multiple background jobs..."
sleep 3 &
sleep 1 &
sleep 2 &

echo "Waiting for all sleep commands to finish."
wait
echo "All jobs are done. Continuing with the rest of the script."
```

### Output:

```bash
$ bash wait_all_example.sh
Starting multiple background jobs...
Waiting for all sleep commands to finish.
(after about 3 seconds)
All jobs are done. Continuing with the rest of the script.
```
Loading