Variables:
#!/bin/bash
echo "Setup and configure server"
file_name=config.yaml
config_files=$(lsconfig)
echo "using file$file_name to configure something"
echo "here are all configuration files:$config_files"
Conditions:
#!/bin/bash
echo "Setup and configure server"
file_name=config.yaml
config_dir=$1
if [ -d "$config_dir" ]
then
echo "reading config directory contents"
config_files=$(ls "$config_dir")
else
echo "config dir not found. Creating one"
mkdir "$config_dir"
touch "$config_dir/config.sh"
fi
# example conditional for checking file
# if [ -f "config.yaml" ]
# example conditional for checking numbers
# num_files=xx
# if [ "$num_files" -eq 10 ]
# example conditional for checking strings
user_group=$2
if [ "$user_group" == "nana" ]
then
echo "configure the server"
elif [ "$user_group" == "admin" ]
then
echo "administer the server"
else
echo "No permission to configure server. wrong user group"
fi
echo "using file$file_name to configure something"
echo "here are all configuration files:$config_files"
User input:
#!/bin/bash
echo "Reading user input"
read -p "Please enter your password: " user_pwd
echo "thanks for your password$user_pwd"
Script Parameters:
#!/bin/bash
echo "all params:$*"
echo "number of params: $#"
echo "user$1"
echo "group$2"
Executing with script parameters:
./example.sh name lastname # 2 params
./example.sh "name lastname" # 1 param
bash example name lastname
Loops:
#!/bin/bash
echo "all params:$*"
echo "number of params: $#"
forparam in $*
do
if [ -d "$param" ]
then
echo "executing scripts in the config folder"
ls -l "$param"
fi
echo $param
done
sum = 0
whiletrue
do
read -p "enter a score" score
if [ "$score" == "q" ]
then
break
fi
sum=$(($sum+$score))
echo "total score:$sum"
done