Posts Linux bash loop pn user input with if
Post
Cancel

Linux bash loop pn user input with if

  • Create an empty file with .sh extension
1
$ touch test.sh
  • Use your favourite editor add a method to the file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ vi test.sh

#!/bin/bash

function loop_on_user_input(){
    for input in ${1} ; do 
        if [ $input = "D" ]; then
           echo "Hello my special one $input"
        else
           echo "Your input is $input"
        fi
    done
}

"$@"

”$@” is what that makes the script file (.sh) be able to call methods

  • Make file executable
1
$ chmod u+x test.sh
  • Call method with space separated multiple inputs
1
2
3
4
5
$ ./test.sh loop_on_user_input 'A B C D'
Your input is A
Your input is B
Your input is C
Hello my special one D
This post is licensed under CC BY 4.0 by the author.