Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
isi-formation-unix
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ISI
isi-formation-unix
Commits
e5aa7727
Commit
e5aa7727
authored
4 months ago
by
alexis.dereeper_ird.fr
Browse files
Options
Downloads
Patches
Plain Diff
Update bash-9-scripting.md
parent
3301dca2
No related branches found
No related tags found
No related merge requests found
Pipeline
#39826
passed
4 months ago
Stage: deploy
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
docs/pages/bash/bash-9-scripting.md
+38
-2
38 additions, 2 deletions
docs/pages/bash/bash-9-scripting.md
with
38 additions
and
2 deletions
docs/pages/bash/bash-9-scripting.md
+
38
−
2
View file @
e5aa7727
...
...
@@ -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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment