Centos Bash Shell Scripts

From rbachwiki
Jump to navigation Jump to search

List Environment Variables

set
# you can | to grep to filter
or
env
Escape Sequence
Test Statements

Environmental Files

Files that execute each time a user logs in to bash shell

/etc/profile 
/etc/bashrc 
~/.bashrc 
~/.bash_profile 
~/.bash_login 
~/.profile

The BASH runtime configuration files (/etc/bashrc and ~/.bashrc) are typically used to set aliases and variables that must be present in the BASH shell. They are executed immediately after a new login as well as when a new BASH shell is created after login. The /etc/bashrc file contains aliases and variables for all users on the system, whereas the ~/.bashrc file contains aliases and variables for a specific user.
The other environment files are only executed after a new login. The /etc/profile file is exe- cuted after login for all users on the system and sets most environment variables, such as HOME and PATH. After /etc/profile finishes executing, the home directory of the user is searched for the hidden environment files. bash_profile, .bash_login, and .profile. If these files exist, the first one found is executed; as a result, only one of these files is typically used. These hidden environment files allow a user to set customized variables independent of BASH shells used by other users on the system; any values assigned to variables in these files override those set in /etc/profile, /etc/bashrc, and ~/.bashrc due to the order of execution

Structure of a bash Script

#!/bin/bash 
echo -e "Today’s date is: \c" 
date echo –e "\nThe people logged into the system include:" 
who 
echo –e "\nWould you like to see the contents of /?(y/n)--> \c" 
read ANSWER if [ $ANSWER = "y" ] 
then echo –e "\nThe contents of the / directory are:" 
ls –F / 
fi

Case Construct

For Loop

#!/bin/bash
echo –e "What directory has the files that you would like to rename? --> \c" 
read DIR
 for NAME in $DIR/* 
do mv $NAME $NAME.txt