Basic Linux Shell Scripts
I. Basic Understanding about Linux Shell Scripts
1. Basic Understand about Linux Shell Scripts
- On Linux, the best way to control Linux is from command interface, it can support you with more options than GUI interface.
- So if you want to learn Linux, you should learn command-line.
- Commands on Linux based on the BASH (Bourne Agian Shell).
- By default, in each user’s home directory, we will have some files by default such as .bash_history, .bash_logout, .bash_profile, and .bashrc. To view these hidden files we have to use “ls -a” command.
- .bash_history : all history for your bash commands.
- .bash_logout : sets command when we exit a shell. By default, we can set “clear” command to clear all previous command on terminal.
- .bash_profile : Calls .bashrc file for more configuration data. Add ~/bin directory to your PATH.
- .bashrc : Calls the /etc/bashrc file for basic configuration data. For root user, this file adds aliases for rm, mv and cp commands to help prevent accident deletion of a file.
2. Redirecting Input and Output
- Standard input comes from a keyboard normally, but when you have a full of data, you don’t need to type everything agian – you can simply redirect that file with <.
- Example: # database_program < database_data
- If you want to save startdard output to a file. You can using this >.
- Example: ls -lah / > folderlist
- If you want to append data to folderlist, you have to use >>.
- Example: ls -lah /var >> folderlist
- We also can combine that
- Example: database_program < database_data > database_output
- Standard error output can help us to troubleshooting the problem
- Example: database_program < database_data 2> error_log
3. Input and Output Pipes
- The pipe (|) can combine the commands.
- Example: ls -lah | more
- The pipe takes the stardard ouput from ls -l command and sends the result as stardard input to more command. So we don’t need to create and delete any temporary files.
4. Running Bash Shell in Background
- Linux is a multitasking system, you can run multiple programs from a single command.
- 2 ways we can make programs run in background command. Example you wrote one shell script backup.sh and you want to run that in background.
- Example: ./backup.sh &
- The ampersand (&) sends program execution to the “background”.
- Ctrl+Z command suspends a running program.
- bg command to send the command to background.
- You can return the program the foreground by fg command.
5. Tildes and Home Directory
- Tilde (~) presents the home directory of current logged-on user.
- We can use the tilde with most of bash shell. Example, users can go to home directory by cd ~ command or list all files in home directory by ls ~ command.
- A single dot (.) represents the current directory.
- A double dot (..) can help us to navigate the parent directory.
- We also can use dot to run a bash script in current directory. Example: ./iptables status
- The characters are the asterisk (*) and the question mark (?). The asterisk (*) presents any numbers or any letters. Each question mark represents one alphanumberic characters.
- Example: ls b* . List all files with first character is b.
- Example: ls b? . List all file with 2 characters and the first character is b.
- We can define special characters with more detail with brackets .
- Example: ls n[1-6].txt List all files with n is the first character and second is from 1 –> 6.
- Example: ls n.txt . List all files with n is the first character and second is from 1 –> 6.
8. Slashes in the Shell.
- We have 2 types: forward slashes (/) and backslashes (\).
- A single forward slash (/) represents the root directory on Linux. Forward slash also help you to navigate to the subdirectories /var/log.
- Backslash help you to escapes the meaning of the character in the shell scripts.
- Example: ls /mnt/windows/My Documents –> will not work in Linux. We have to use ls /mnt/windows/My\ Documents
- Single quotes (‘) : The shell does not process any variables or commands.
- Double quotes (“): The shell processes variables, such as $NAME, but does not process any commands.
- Back quotes (‘) : The shell tries to process every word in quotes as a command.
- Example with single quotes:
- Example with Double quotes:
- Example with Back quotes:
- Aliases is one of the most useful shell variables.
- alias command will show you all list of commands that we can use.
Example to configure one alias on Linux System
If you don’t want to use that alias
10. Shell Conditionals
If we want to build a shell scripts, the most basic of these is the conditinal also known as an if conditional.
- if [ x -eq y ] : If x=y?
- if [ x -ne y ] : If x#y?
- if [ x -gt y ] : If x > y ?
- if [ x -ge y ] : If x >= y ?
- if [ x -lt y ] : If x < y ?
- if [ x -le y ] : If x <= y ?
- if [ -s /etc/file ] : if /etc/file have data ?
- if [ -f /etc/file ] : if /etc/file is a regular file ?
- if [ -d /etc/dir ] : if /etc/dir is directory ?
- if [ x -ne y -a x -lt z] : if x# y and x < z ?
11. Write a sample shell script
- Create shell script for calculator
- Change permission to activate execute permission
- Run shell script
II. Some Linux / Unix Command Examples in Practice
1. Top 10 Basic Linux / Unix Commands
2. 30 best commands used by Linux / Unix Administrator
Thanks for using ITHelpblog.com.