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

Update bash-9-scripting.md

parent 261f5e41
No related branches found
No related tags found
No related merge requests found
Pipeline #40499 passed
......@@ -108,5 +108,35 @@ echo $var2
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
```
```
\ No newline at end of file
!!! 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"
\ 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