Skip to content
Snippets Groups Projects
bash-9-scripting-corrected.md 3.88 KiB
Newer Older
alexis.dereeper_ird.fr's avatar
alexis.dereeper_ird.fr committed
# Scripting



## Comment

In bash, everything after the `#` special character is a comment and will be skipped.
You can check it by yourself:

```bash
echo hello # how are you? my command ls ; cd 
```

When it will come the time to write scripts, you will have to use it extensively to comment your code...

## Variable

A variable is a named storage location that holds a value or information. Think of a variable as a container. The name of the variable allows you to refer to and access the stored value.

In bash any character or string can become a variable storing any value using the = sign as follow:

```bash
v=hello
var=4
echo $v
echo $var
```

!!! question "Try to define a variable `number` and initialize it to 10. Display the value of the variable with `echo`. Change its value to 20 and display it"

??? example "Click to show the solution"
    ```bash
    number=10
    echo $number
    number=20
    echo $number
    ```

## First script

!!! question "Write a bash script called `count.sh` that allows to count the number of files contained in the directory `training_unix_basics/data`"

??? example "Click to show the solution"
    ```bash
    #!/bin/bash

    # directory variable
    directory=/home/<user_name>/training_unix_basics/data

    # count the number of files
    file_count=$(ls -1 "$directory" | wc -l)

    # display the result
    echo $file_count
    ```

!!! question "Make the script executable with the `chmod` command and run it"

??? example "Click to show the solution"
    ```bash
    chmod +x count.sh
    ./count.sh
    ```

## Read the standard input

`read` is a bash built-in command that reads a line from the standard input (or from the file descriptor) and split the line into words. The first word is assigned to the first variable, the second one to the second variable, and so on.

```bash
read var1 var2
Hello world
echo $var1
echo $var2
```

!!! question "Write a bash script called `count2.sh` that asks for a directory name and count the number of files contained"

??? example "Click to show the solution"
    ```bash
    #!/bin/bash

    # Ask the user to enter the name of a directory
    echo -n "Enter the path of the directory to analyze : "
    read directory

    # count the number of files
    file_count=$(ls -1 "$directory" | wc -l)

    # display the result
    echo $file_count
    ```

## Script with argument

!!! question "Write a bash script called `count3.sh` that takes as an argument a directory name and count the number of files contained, after checking that the directory exists"

??? example "Click to show the solution"
    ```bash
    #!/bin/bash

    # directory variable
    directory=$1

    if [ -d "$directory" ]; then
    # count the number of files
     file_count=$(ls -1 "$directory" | wc -l)
     echo "Directory '$directory' contains $file_count files."
    else
      echo "Directory '$directory' does not exist."
    fi
    ```

## For loop

!!! question "Write a bash script called `count4.sh` that does the same as previously, and also report the number of characters for each file"

??? example "Click to show the solution"
    ```bash
    #!/bin/bash

    # directory variable
    directory=$1

    if [ -d "$directory" ]; then
    # count the number of files
     file_count=$(ls -1 "$directory" | wc -l)
     echo "Directory '$directory' contains $file_count files."
     for file in `ls $directory`
      do
       if [ -f "$directory/$file" ]; then
        number_lines=$(cat "$directory/$file" | wc -c)
        echo $file $number_lines
       fi
      done
    else
      echo "Directory '$directory' does not exist."
    fi
    ```


## Increment a variable

!!! question "Write a bash script called `count5.sh` that does the same as previously, and also report the total number of characters contained in all files"