Browse Source

feat(in-the-dark): add subject, test and solution for the exercise

DEV-4397-piscine-ai-missing-file-for-ex-7-of-nlp
miguel 2 years ago committed by MSilva95
parent
commit
15a88f5137
  1. 31
      sh/tests/in-the-dark_test.sh
  2. 2
      sh/tests/solutions/in-the-dark.sh
  3. 59
      subjects/devops/in-the-dark/README.md

31
sh/tests/in-the-dark_test.sh

@ -0,0 +1,31 @@
#!/usr/bin/env bash
# Unofficial Bash Strict Mode
set -euo pipefail
IFS='
'
FILENAME="student/in-the-dark.sh"
script_dirS=$(cd -P "$(dirname "$BASH_SOURCE")" &>/dev/null && pwd)
challenge() {
submitted=$(cd "$1" && bash "$script_dirS"/$FILENAME)
expected=$(cd "$1" && bash "$script_dirS"/solutions/in-the-dark.sh)
diff <(echo "$submitted") <(echo "$expected")
}
# True if FILE exists and is a regular file
if [ -f ${FILENAME} ]; then
# FILE exists and it's not empty
if [ -s ${FILENAME} ]; then
challenge ../../subjects
challenge .
else
echo "The file exist but is empty"
exit 1
fi
else
echo "File does not exist"
exit 1
fi

2
sh/tests/solutions/in-the-dark.sh

@ -0,0 +1,2 @@
ls -R | grep "\.txt$" && echo "Search complete" >new &
jobs

59
subjects/devops/in-the-dark/README.md

@ -0,0 +1,59 @@
## in-the-dark
### Instructions
Create a script `in-the-dark.sh` which will execute a job in the background that will run the command `ls` which will recursively list all files and directories in the current directory and its subdirectories. The output will be piped into the `grep` command, which will search for all files with the `.txt` extension. After the search is complete, you must redirect to a file "new" the following string "Search complete". You also need to List all background jobs.
You must do all these steps running only one job!
Expected output:
```console
$ ./in-the-dark.sh
[1]+ Running "Your running job"
news_amazon.txt
model_forecasts.txt
Ecommerce_purchases.txt
breast_cancer_readme.txt
shadow.txt
standard.txt
thinkertoy.txt
$ cat new
Search complete
$
```
### Hints
To run the `ls` command recursively in the background on a lot of files, you can use the `-R` option which will recursively list all files and directories in the current directory and its subdirectories. The output must be piped into the grep command. Here is a similar example.
```console
$ ls
is-winner
printalphabetalt
printcomb2
question_mark
onlyz
organize_garage
tetris-optimizer
unzipstring
$ ls | grep "z"
onlyz
organize_garage
tetris-optimizer
unzipstring
```
The `&&` operator is used to combine multiple commands, and execute them as a single command. It is often used to run multiple commands in sequence, where the second command only runs if the first command succeeds.
```console
$ touch newfile.txt && chmod 755 newfile.txt
$ ls -l
-rwxr-xr-x 1 user user 0 jan 2 17:29 newfile.txt
$
```
This will run the `touch` command, which will create a new file called "newfile.txt". If the touch command succeeds, the `chmod` command will run and change the permissions of "newfile.txt" to allow the owner to read, write, and execute the file, and allow everyone else to read and execute the file.
> You have to use Man or Google to know more about commands flags, in order to solve this exercise!
> Google and Man will be your friends!
Loading…
Cancel
Save