Skip to content
Snippets Groups Projects
Commit cf3380d4 authored by alexis.dereeper_ird.fr's avatar alexis.dereeper_ird.fr
Browse files

Update bash-9-scripting.md

parent e5aa7727
No related branches found
No related tags found
No related merge requests found
Pipeline #39827 passed
......@@ -56,18 +56,21 @@ When it will come the time to write scripts, you will have to use it extensively
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"
!!! 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=/home/<user_name>/training_unix_basics/data
directory=$1
if [ -d "$directory" ]; then
# count the number of files
file_count=$(ls -1 "$directory" | wc -l)
file_count=$(ls -1 "$directory" | wc -l)
echo "Directory '$directory' contains $file_count files."
else
echo "Directory '$directory' does not exist."
fi
# display the result
echo $file_count
```
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment