From e5aa772764b0e7b3631fe45b1e77909f7b5f36f0 Mon Sep 17 00:00:00 2001 From: "alexis.dereeper_ird.fr" <alexis.dereeper@ird.fr> Date: Sat, 23 Nov 2024 16:35:25 +0000 Subject: [PATCH] Update bash-9-scripting.md --- docs/pages/bash/bash-9-scripting.md | 40 +++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/docs/pages/bash/bash-9-scripting.md b/docs/pages/bash/bash-9-scripting.md index fa13131..34dac85 100644 --- a/docs/pages/bash/bash-9-scripting.md +++ b/docs/pages/bash/bash-9-scripting.md @@ -15,14 +15,14 @@ When it will come the time to write scripts, you will have to use it extensively ## First scripts -!!! question "Write a bash script called `count.sh` that allows to count the number of files contained in the directory `training_unix_basics`" +!!! 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 + directory=/home/<user_name>/training_unix_basics/data # count the number of files file_count=$(ls -1 "$directory" | wc -l) @@ -31,7 +31,43 @@ When it will come the time to write scripts, you will have to use it extensively 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 + ``` + !!! 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 + ``` + !!! question "Write a bash script called `count3.sh` that takes as an argument a directory name and count the number of files contained" +??? 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 + ``` \ No newline at end of file -- GitLab