From 4719e5d451660d09432289453cfbd5c552574bd0 Mon Sep 17 00:00:00 2001 From: waldek Date: Mon, 23 May 2022 16:51:00 +0200 Subject: [PATCH] more scripting --- advanced/learning_bash_scripting.md | 58 ++++++++++++++++++++++++++++- advanced/learning_shells.md | 4 ++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/advanced/learning_bash_scripting.md b/advanced/learning_bash_scripting.md index 822454d..268c7b8 100644 --- a/advanced/learning_bash_scripting.md +++ b/advanced/learning_bash_scripting.md @@ -732,7 +732,6 @@ waldek@debian:~$ which true /usr/bin/true waldek@debian:~$ which false /usr/bin/false -waldek@debian:~$ man true waldek@debian:~$ whatis true true (1) - do nothing, successfully waldek@debian:~$ whatis false @@ -765,6 +764,8 @@ waldek@debian:~$ echo $? waldek@debian:~$ test -a does_not_exist waldek@debian:~$ echo $? 1 +waldek@debian:~$ test ! -a does_not_exist; echo $? +0 waldek@debian:~$ ``` @@ -1108,6 +1109,10 @@ whale says: I'm an animal... waldek@debian:~$ ``` +## `break` and `continue` + +TODO + # Coding challenge - pipe or argument plus action! Can you expand the previous coding challenge, where you perform a conditional logic on the input of the script and actually *use* the incoming data? @@ -1118,6 +1123,52 @@ Rename all files from [this](../assets/simple_sort_01.tar.gz) file with an prefi Can you give the files that start with an **uppercase** letter a different pre or postfix? Can you move the files with lowercase into a different folder? +# Coding challenge - Guess the number + +Can you make me a small guessing game like the one below? + +``` +waldek@debian:~$ ./test.sh +I have a number in mind between 0 and 100 +your quess: 10 +my number is smaller... +your quess: 5 +my number is bigger... +your quess: helloworld +I don't understand you... +your quess: 6 +disco! I had 6 in mind... +waldek@debian:~$ +``` + +
+ +Spoiler warning! + +```bash +#!/bin/bash + +difficulty=100 +computernumber=$(( "$RANDOM % $difficulty")) +echo "I have a number in mind between 0 and $difficulty" + +while true; do + read -p "your quess: " usernumber + if [ $usernumber -lt $computernumber ] 2> /dev/null; then + echo "my number is bigger..." + elif [ $usernumber -gt $computernumber ] 2> /dev/null; then + echo "my number is smaller..." + elif [ $usernumber -eq $computernumber ] 2> /dev/null; then + echo "disco! I had $computernumber in mind..." + break + else + echo "I don't understand you..." + fi +done +``` + +
+ # Functions - Reuse code to make life easier. ## defining a function @@ -1133,10 +1184,15 @@ Can you move the files with lowercase into a different folder? # User Interface - Make your scripts user friendly. * `case` for command line arguments +* `select` for small menu's * [dialog tutorial](https://www.linuxjournal.com/article/2807) * [better dialog tutorial](https://linuxcommand.org/lc3_adv_dialog.php) * [Ryan's tutorials](https://ryanstutorials.net/bash-scripting-tutorial/bash-user-interfaces.php) +# Arrays in `bash` + +TODO + ## Python [This](https://gitea.86thumbs.net/waldek/python_course_doc) repository has a twenty day course to learn python written by me. diff --git a/advanced/learning_shells.md b/advanced/learning_shells.md index 83ad6c3..ef5c416 100644 --- a/advanced/learning_shells.md +++ b/advanced/learning_shells.md @@ -26,6 +26,7 @@ TODO TODO + # Shell completion TODO @@ -42,3 +43,6 @@ TODO TODO +# `xxh` + +TODO