Comment formater les chaînes de caractères dans les scripts Bash

ShellShellBeginner
Pratiquer maintenant

💡 Ce tutoriel est traduit par l'IA à partir de la version anglaise. Pour voir la version originale, vous pouvez cliquer ici

Introduction

In the world of Bash scripting, the ability to format strings is a crucial skill. This tutorial will guide you through the fundamentals of Bash string formatting, from basic techniques to more advanced applications. By the end of this lab, you will be able to effectively manipulate and present data in your Bash scripts, making them more readable and user-friendly.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("Shell")) -.-> shell/VariableHandlingGroup(["Variable Handling"]) shell(("Shell")) -.-> shell/ControlFlowGroup(["Control Flow"]) shell(("Shell")) -.-> shell/BasicSyntaxandStructureGroup(["Basic Syntax and Structure"]) shell/BasicSyntaxandStructureGroup -.-> shell/quoting("Quoting Mechanisms") shell/VariableHandlingGroup -.-> shell/variables_decl("Variable Declaration") shell/VariableHandlingGroup -.-> shell/variables_usage("Variable Usage") shell/VariableHandlingGroup -.-> shell/str_manipulation("String Manipulation") shell/VariableHandlingGroup -.-> shell/param_expansion("Parameter Expansion") shell/ControlFlowGroup -.-> shell/if_else("If-Else Statements") shell/ControlFlowGroup -.-> shell/case("Case Statements") subgraph Lab Skills shell/quoting -.-> lab-400162{{"Comment formater les chaînes de caractères dans les scripts Bash"}} shell/variables_decl -.-> lab-400162{{"Comment formater les chaînes de caractères dans les scripts Bash"}} shell/variables_usage -.-> lab-400162{{"Comment formater les chaînes de caractères dans les scripts Bash"}} shell/str_manipulation -.-> lab-400162{{"Comment formater les chaînes de caractères dans les scripts Bash"}} shell/param_expansion -.-> lab-400162{{"Comment formater les chaînes de caractères dans les scripts Bash"}} shell/if_else -.-> lab-400162{{"Comment formater les chaînes de caractères dans les scripts Bash"}} shell/case -.-> lab-400162{{"Comment formater les chaînes de caractères dans les scripts Bash"}} end

Understanding Basic String Usage in Bash

In this first step, we will explore how strings work in Bash and learn about the most basic forms of string usage.

What is a String in Bash?

In Bash, a string is simply a sequence of characters. Strings can be enclosed in single quotes (') or double quotes ("), and each quoting method has its own behavior.

Let's open our first script file to start working with strings:

  1. Open the file basic_string.sh in the editor:

    • Click on the "Explorer" icon in the sidebar
    • Navigate to ~/project/string_formatting
    • Click on basic_string.sh to open it
  2. Replace the contents with the following code:

#!/bin/bash

## Single quotes example - variables are not expanded
echo 'This is a string with single quotes'

## Double quotes example - variables can be expanded
echo "This is a string with double quotes"

## Creating a variable
name="LabEx User"

## Using a variable with single quotes (will not expand)
echo 'Hello, $name!'

## Using a variable with double quotes (will expand)
echo "Hello, $name!"
  1. Save the file by pressing Ctrl+S or selecting "File" > "Save" from the menu

  2. Now let's execute the script in the terminal:

    • Open a terminal if it's not already open
    • Navigate to our working directory:
      cd ~/project/string_formatting
    • Run the script:
      ./basic_string.sh

You should see output similar to this:

This is a string with single quotes
This is a string with double quotes
Hello, $name!
Hello, LabEx User!

Notice the difference between single quotes and double quotes. Single quotes preserve the literal value of each character, while double quotes allow for variable expansion. This is a fundamental concept in Bash string formatting.

Combining Strings

Let's add to our script to see how strings can be combined in Bash:

  1. Add the following code to the end of basic_string.sh:
## Combining strings
first_name="LabEx"
last_name="User"

## Method 1: Using variable expansion within double quotes
full_name="$first_name $last_name"
echo "Full name: $full_name"

## Method 2: Simple concatenation
greeting="Welcome, "$first_name" "$last_name"!"
echo "$greeting"
  1. Save the file and run it again:
./basic_string.sh

You should now see additional output:

Full name: LabEx User
Welcome, LabEx User!

Understanding String Length

Another basic operation is determining the length of a string. Let's add this to our script:

  1. Add the following code to the end of basic_string.sh:
## Getting string length
message="This is a test message."
length=${#message}
echo "The message: $message"
echo "Length of the message: $length characters"
  1. Save the file and run it again:
./basic_string.sh

The additional output should be:

The message: This is a test message.
Length of the message: 23 characters

This covers the basics of string usage in Bash. Understanding these fundamentals will prepare you for more advanced string formatting techniques that we will explore in the next steps.

Working with Variables in Strings

In this step, we will delve deeper into using variables within strings and learn about different techniques for variable substitution in Bash.

Variable Substitution Basics

Variable substitution is one of the most common string formatting techniques in Bash. Let's explore various ways to insert variables into strings:

  1. Open the file variable_demo.sh in the editor:

    • Navigate to ~/project/string_formatting in the file explorer
    • Click on variable_demo.sh to open it
  2. Replace the contents with the following code:

#!/bin/bash

## Basic variable
username="labex"
directory="/home/$username"

echo "User directory: $directory"

## Using curly braces for clarity
file_type="txt"
echo "Looking for files ending with .${file_type}"

## Variable substitution adjacent to text
count=5
echo "Found $countfiles"   ## This won't work as expected
echo "Found ${count}files" ## This works correctly
  1. Save the file by pressing Ctrl+S or selecting "File" > "Save" from the menu

  2. Now let's execute the script:

    ./variable_demo.sh

You should see output similar to:

User directory: /home/labex
Looking for files ending with .txt
Found
Found 5files

Notice how the first attempt to print $countfiles failed to display the number because Bash looked for a variable named countfiles which doesn't exist. Using curly braces ${count} clearly defines where the variable name ends.

Advanced Variable Substitution

Let's explore more advanced variable substitution techniques:

  1. Add the following code to the end of variable_demo.sh:
## Default values for variables
## ${variable:-default} uses default if variable is unset or empty
echo "Welcome, ${visitor:-Guest}!"

## Set a variable
visitor="John"
echo "Welcome, ${visitor:-Guest}!"

## ${variable:=default} sets variable to default if it's unset or empty
unset visitor
echo "Welcome, ${visitor:=Guest}!"
echo "Now visitor is set to: $visitor"

## Parameter expansion with substrings
message="Welcome to Bash scripting"
## Extract first 7 characters
echo "${message:0:7}" ## Output: Welcome

## Extract from position 11 to the end
echo "${message:11}" ## Output: Bash scripting
  1. Save the file and run it again:
    ./variable_demo.sh

The additional output should show:

Welcome, Guest!
Welcome, John!
Welcome, Guest!
Now visitor is set to: Guest
Welcome
Bash scripting

Practical Example: Creating a File Path Generator

Let's create a practical example that uses variable substitution to generate file paths:

  1. Add the following code to the end of variable_demo.sh:
## Practical example: File path generator
username="labex"
project="myproject"
file_name="data"
extension="csv"

## Generate file path using variable substitution
file_path="/home/${username}/projects/${project}/${file_name}.${extension}"
echo "Full file path: $file_path"

## Generate backup file path
backup_path="${file_path}.backup"
echo "Backup file path: $backup_path"

## Timestamp for versioned backups
timestamp=$(date +"%Y%m%d")
versioned_backup="${file_path}.${timestamp}"
echo "Versioned backup: $versioned_backup"
  1. Save the file and run it:
    ./variable_demo.sh

You should see additional output similar to:

Full file path: /home/labex/projects/myproject/data.csv
Backup file path: /home/labex/projects/myproject/data.csv.backup
Versioned backup: /home/labex/projects/myproject/data.csv.20231025

The exact timestamp will differ based on the current date.

This example demonstrates how variable substitution can be used to dynamically generate complex strings, such as file paths, with minimal repetition of code. This technique is particularly useful in scripts that need to work with multiple files or directories.

Adding Color and Style to Bash Output

In this step, we will learn how to enhance your Bash scripts with colorful and styled text. Using color in your script output can improve readability and highlight important information.

Understanding ANSI Escape Sequences

Bash supports ANSI escape sequences to control text formatting, including colors, bold, underline, and more. Let's explore how to use these sequences:

  1. Open the file color_formatting.sh in the editor:

    • Navigate to ~/project/string_formatting in the file explorer
    • Click on color_formatting.sh to open it
  2. Replace the contents with the following code:

#!/bin/bash

## Basic color examples
## Note: we use echo -e to enable interpretation of backslash escapes

## Text colors
echo -e "\033[31mThis text is red\033[0m"
echo -e "\033[32mThis text is green\033[0m"
echo -e "\033[33mThis text is yellow\033[0m"
echo -e "\033[34mThis text is blue\033[0m"
echo -e "\033[35mThis text is magenta\033[0m"
echo -e "\033[36mThis text is cyan\033[0m"

## Reset color at the end with \033[0m to prevent color bleeding
  1. Save the file by pressing Ctrl+S or selecting "File" > "Save" from the menu

  2. Now let's make the file executable and run it:

    chmod +x color_formatting.sh
    ./color_formatting.sh

You should see the text output in different colors. Each line uses a different ANSI color code, and we reset the color at the end of each line with \033[0m.

Using Variables for Better Readability

The ANSI escape sequences can be difficult to read in your code. Let's improve our script by defining variables for each color:

  1. Update the color_formatting.sh file with the following code:
#!/bin/bash

## Define color variables for better readability
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
MAGENTA='\033[35m'
CYAN='\033[36m'
## Reset color
RESET='\033[0m'

## Using color variables
echo -e "${RED}This text is red${RESET}"
echo -e "${GREEN}This text is green${RESET}"
echo -e "${YELLOW}This text is yellow${RESET}"
echo -e "${BLUE}This text is blue${RESET}"
echo -e "${MAGENTA}This text is magenta${RESET}"
echo -e "${CYAN}This text is cyan${RESET}"

## You can also mix colors in a single line
echo -e "This is ${RED}red${RESET}, this is ${GREEN}green${RESET}, and this is ${BLUE}blue${RESET}."
  1. Save the file and run it again:
    ./color_formatting.sh

The output should be the same as before, but the code is now much more readable.

Adding Text Styles

In addition to colors, you can also add styles like bold, underline, and background colors:

  1. Add the following code to the end of color_formatting.sh:
## Text styles
BOLD='\033[1m'
UNDERLINE='\033[4m'
## Background colors
BG_RED='\033[41m'
BG_GREEN='\033[42m'
BG_YELLOW='\033[43m'

## Examples with styles
echo -e "${BOLD}This text is bold${RESET}"
echo -e "${UNDERLINE}This text is underlined${RESET}"
echo -e "${RED}${BOLD}This text is bold and red${RESET}"

## Examples with background colors
echo -e "${BG_RED}This has a red background${RESET}"
echo -e "${BG_GREEN}${BLUE}Blue text on green background${RESET}"
  1. Save the file and run it again:
    ./color_formatting.sh

You should now see text with different styles and background colors.

Practical Example: Formatted Script Output

Let's create a practical example that uses colors and styles to format script output:

  1. Add the following code to the end of color_formatting.sh:
## Practical example: Script status messages
print_info() {
  echo -e "${CYAN}[INFO]${RESET} $1"
}

print_success() {
  echo -e "${GREEN}[SUCCESS]${RESET} $1"
}

print_warning() {
  echo -e "${YELLOW}[WARNING]${RESET} $1"
}

print_error() {
  echo -e "${RED}[ERROR]${RESET} $1"
}

## Using our formatted output functions
print_info "Starting the process..."
print_success "File successfully downloaded."
print_warning "Disk space is running low."
print_error "Failed to connect to the server."

## Simulating a task with progress updates
echo -e "\n${BOLD}Running a sample task:${RESET}"
for i in {1..5}; do
  print_info "Processing step $i..."
  sleep 1
  print_success "Step $i completed."
done

print_success "All tasks completed successfully!"
  1. Save the file and run it:
    ./color_formatting.sh

You will see a series of formatted messages with different colors indicating different types of information. The script will pause briefly between steps due to the sleep command.

This example demonstrates how using colors and styles can make your script output more organized and easier to understand. This is particularly useful for scripts that produce a lot of output or that need to clearly distinguish between different types of messages like information, warnings, and errors.

Advanced String Operations in Bash

In this final step, we will explore more advanced string operations in Bash, including string manipulation, pattern matching, and substitution. These techniques will allow you to perform complex text processing tasks in your scripts.

String Manipulation Functions

Bash provides several built-in mechanisms for manipulating strings. Let's explore these operations:

  1. Open the file string_ops.sh in the editor:

    • Navigate to ~/project/string_formatting in the file explorer
    • Click on string_ops.sh to open it
  2. Replace the contents with the following code:

#!/bin/bash

## String length
message="Hello, Bash scripting world!"
echo "Original message: $message"
echo "Length of message: ${#message} characters"

## Extracting substrings
## Syntax: ${string:position:length}
echo "First 5 characters: ${message:0:5}"
echo "Characters 7-11: ${message:7:5}"
echo "From position 13 to end: ${message:13}"

## Searching and replacing within strings
filename="report.txt.backup"
echo "Original filename: $filename"

## Replace first occurrence
echo "Replace first occurrence of 'txt' with 'pdf': ${filename/txt/pdf}"

## Replace all occurrences
text="one two one two three"
echo "Original text: $text"
echo "Replace all occurrences of 'one' with '1': ${text//one/1}"

## Replace at beginning of string
echo "Replace 'one' at beginning: ${text/#one/1}"

## Replace at end of string
echo "Replace 'three' at end: ${text/%three/3}"
  1. Save the file by pressing Ctrl+S or selecting "File" > "Save" from the menu

  2. Now let's make the file executable and run it:

    chmod +x string_ops.sh
    ./string_ops.sh

You should see output showing various string manipulation operations:

Original message: Hello, Bash scripting world!
Length of message: 29 characters
First 5 characters: Hello
Characters 7-11: Bash
From position 13 to end: scripting world!
Original filename: report.txt.backup
Replace first occurrence of 'txt' with 'pdf': report.pdf.backup
Original text: one two one two three
Replace all occurrences of 'one' with '1': 1 two 1 two three
Replace 'one' at beginning: 1 two one two three
Replace 'three' at end: one two one two 3

Case Conversion

Bash also provides mechanisms for changing the case of text. Let's add case conversion examples:

  1. Add the following code to the end of string_ops.sh:
## Case conversion
uppercase="HELLO WORLD"
lowercase="hello world"
mixed="Hello World"

## Converting to uppercase
echo "Original: $lowercase"
echo "Uppercase: ${lowercase^^}"

## Converting to lowercase
echo "Original: $uppercase"
echo "Lowercase: ${uppercase,,}"

## Converting first character to uppercase
echo "Original: $lowercase"
echo "First character uppercase: ${lowercase^}"

## Converting first character to lowercase
echo "Original: $mixed"
echo "First character lowercase: ${mixed,}"
  1. Save the file and run it again:
    ./string_ops.sh

The additional output should show case conversion examples:

Original: hello world
Uppercase: HELLO WORLD
Original: HELLO WORLD
Lowercase: hello world
Original: hello world
First character uppercase: Hello world
Original: Hello World
First character lowercase: hello World

String Trimming and Formatting

Let's add examples of string trimming and formatting techniques:

  1. Add the following code to the end of string_ops.sh:
## String trimming
path="  /usr/local/bin/  "
echo "Original path with spaces: '$path'"
echo "Trimmed path: '${path// /}'" ## Remove all spaces

## Removing prefixes and suffixes
filename="archive.tar.gz"
echo "Original filename: $filename"
echo "Without .tar.gz extension: ${filename%.tar.gz}"
echo "Without any extension: ${filename%%.*}"
echo "Extract extension: ${filename#*.}"
echo "Extract full extension: ${filename##*.}"

## Padding strings
number=42
printf "Number with leading zeros (5 digits): %05d\n" $number

name="Bob"
printf "Name padded to 10 characters: %-10s|\n" $name
  1. Save the file and run it again:
    ./string_ops.sh

The additional output should show trimming and formatting examples:

Original path with spaces: '  /usr/local/bin/  '
Trimmed path: '/usr/local/bin/'
Original filename: archive.tar.gz
Without .tar.gz extension: archive
Without any extension: archive
Extract extension: tar.gz
Extract full extension: gz
Number with leading zeros (5 digits): 00042
Name padded to 10 characters: Bob       |

Practical Example: Log Parser

Let's create a practical example that uses advanced string operations to parse and format log entries:

  1. Add the following code to the end of string_ops.sh:
## Practical example: Log parser
echo -e "\n--- Log Parser Example ---\n"

## Sample log entries
log_entries=(
  "[2023-10-25 08:15:22] INFO: Application started"
  "[2023-10-25 08:16:45] WARNING: High memory usage detected"
  "[2023-10-25 08:17:30] ERROR: Database connection failed"
  "[2023-10-25 08:18:10] INFO: Backup process initiated"
  "[2023-10-25 08:19:55] ERROR: Out of disk space"
)

## Parse and format log entries
for entry in "${log_entries[@]}"; do
  ## Extract timestamp (everything between [ and ])
  timestamp=${entry#*[}
  timestamp=${timestamp%%]*}

  ## Extract log level (INFO, WARNING, ERROR)
  level=${entry#*] }
  level=${level%%:*}

  ## Extract message
  message=${entry#*: }

  ## Format based on log level
  case $level in
    "INFO")
      printf "%-20s [\033[36m%-7s\033[0m] %s\n" "$timestamp" "$level" "$message"
      ;;
    "WARNING")
      printf "%-20s [\033[33m%-7s\033[0m] %s\n" "$timestamp" "$level" "$message"
      ;;
    "ERROR")
      printf "%-20s [\033[31m%-7s\033[0m] %s\n" "$timestamp" "$level" "$message"
      ;;
  esac
done
  1. Save the file and run it:
    ./string_ops.sh

The final output section should show formatted log entries with different colors based on log level:

--- Log Parser Example ---

2023-10-25 08:15:22  [INFO   ] Application started
2023-10-25 08:16:45  [WARNING] High memory usage detected
2023-10-25 08:17:30  [ERROR  ] Database connection failed
2023-10-25 08:18:10  [INFO   ] Backup process initiated
2023-10-25 08:19:55  [ERROR  ] Out of disk space

This log parser example demonstrates how combining various string operations with formatting and color codes can transform raw text data into a more readable and structured format. Such techniques are extremely useful when developing scripts for log analysis, data processing, or reporting.

Summary

In this lab, you've learned essential techniques for formatting strings in Bash scripts. Here's a summary of what you've accomplished:

  1. Basic String Usage: You learned about basic string handling in Bash, including the difference between single and double quotes, string concatenation, and determining string length.

  2. Variable Substitution: You explored various methods of variable substitution, including basic substitution, using curly braces for clarity, setting default values, and extracting substrings.

  3. Color and Style Formatting: You discovered how to enhance your script output with colors and text styles using ANSI escape sequences, making your script output more readable and user-friendly.

  4. Advanced String Operations: You mastered advanced string manipulation techniques such as substring extraction, pattern matching, replacement, case conversion, and string trimming.

These string formatting skills will significantly improve your Bash scripting capabilities, allowing you to create more readable, maintainable, and user-friendly scripts. As you continue your Bash scripting journey, you'll find these techniques invaluable for building robust command-line tools and automation scripts.