scripts-main/functions.sh

81 lines
2.6 KiB
Bash
Raw Normal View History

2019-09-14 06:57:37 -05:00
#!/bin/bash
######################################################
## filename: functions.sh
## Description: Learn how to use functions in bash
## Notes: Fucntions have to be called or 'run' to use them
## Run a function by typing it's name
## variable names such as 'user_input' can be named anything
## you cannot run a function before you create/defne it
#####################################################
slap(){
echo -n "who wants to get slapped: " # use echo command without automatic newline
read user_input # "read": reads what user typed and store in variable called 'user_input'
# use double brackets to make code run more reliably in bash (does not work in other shells)
if [[ $user_input = "me" ]];then
echo "you gets a slap"
elif [[ $user_input = "you" ]];then
echo "you still get slapped"
fi
}
block_tutorial(){
# Show how to test if something is a device or not
echo -n "enter a block device name: " # ask and print the question
read user_input # read the users response and puts it into the variable "user_input"
if [[ -b $user_input ]];then # the user enters a device name and -b tests if it is a block device
echo "This is a block device"
elif [[ ! -b $user_input ]];then # "!" specifically checks if user_input is NOT a block device
echo "This is not a block device"
fi
}
file_tutorial(){
# Show how to test if something is a file or not
echo -n "enter file name: " #asks for a filename to test
read user_input # reads the users response and puts it into the variable "user_input"
if [[ -f $user_input ]];then
echo "This is a file"
elif [[ ! -f $user_input ]];then
echo "This is not a file"
fi
}
folder_tutorial(){
#Show if something is a folder or not
echo -n "enter folder path: " #asks for folder path
read user_input # reads the users response and puts it into the variable "user_input"
if [[ -d $user_input ]];then
echo "this is a folder"
elif [[ ! -f $user_input ]];then
echo "this is not a folder"
fi
2019-09-14 06:57:37 -05:00
}
empty_variable(){
# Show if a variable it empty or not
echo -n "Enter some text: " #asks user to enter random text
read some_text # read what user typed and store it into variable called "some_text"
if [[ -z $some_text ]];then
echo "There is nothing stored in the varialbe 'some_text'"
elif [[ ! -z $some_text ]];then
echo "The user has entered in some text!"
fi
2019-09-14 06:57:37 -05:00
}
# run functions created above or comment them to stop them from running
slap
block_tutorial
file_tutorial
folder_tutorial
empty_variable