Bash script improvements
The conditional statement
1 | # Check if Java is installed and set the path |
Limit execution
Place logic within a script using Linux environment variables
Env var | Description |
---|---|
$USER | provides the username |
$UID | provides the user’s identification number (UID) of the executing user |
User
1
2
3
4
5
6# only user JBOSS1 can run this script
if [ "$USER" != 'jboss1' ]; then
echo "Sorry, this script must be run as JBOSS1!"
exit 1
fi
echo "continue script"User identification name
1
2
3
4
5
6# only user ROOT can run this script, whose UID is 0
if [ "$UID" -gt 0 ]; then
echo "Sorry, this script must be run as ROOT!"
exit 1
fi
echo "continue script"
Use arguments
No arguments
1
2
3
4
5
6# Don't do anything if there is no argument
if [ $# -eq 0 ]; then
echo "No arguments provided"
exit 1
fi
echo "arguments found: $#"Multiple arguments
1
echo $1 $2 $3
Agument 0 is the name of the script being executed
1
2# log the name of the script executed
echo test >> $0.log
User input
Offer user input
1
2
3echo "enter a word please:"
read word
echo $wordOffer choices to the use*
1
2
3
4
5read -p "Install Software ?? [Y/n]: " answ
if [ "$answ" == 'n' ]; then
exit 1
fi
echo "Installation starting..."
Exit on failure
1 | #install JDK8: extract the file and check success before setting symbolic links |