ملخص كتابة سكربت باش - Bash Shell Script Cheatsheet

Shell Scripting Crash Course - Cheatsheet for beginners

This is a bash shell scripting which can be found on Unix, Linux and Mac. You can install bash on the Linux subsystem on Windows too.

the first line is #! /bin/bash because the bash program is in /bin/bash, you can know the path where the bash is by this command which bash

print data or text on the screen

You can useecho Hello, World!or echo "Hello, world!"

Variables & Print out them

or you can use this

Get Data From User

If Statement

The simple if statement syntax of bash script:

NOTE THAT: the end of if is fi the reverse letters of if.

The if else statement syntax in bash script:

The else if (elif) condition statement syntax in bash scripting:

Logic Comparisons

You can use those operators:

Logic OperatorMeaning
-eqequal to (the same meaning of == in other programming languages)
-nenot equal (the same meaning of != in other programming languages)
-gtgreater then (the same meaning of > in other programming languages)
-gegreater than or equal to (the same meaning of >= in other programming languages)
-ltless than (the same meaning of < in other programming languages)
-leless than or equal to (the same meaning of <= in other programming language)

and use them like this:

File Conditions

these are the file condition flags:

symbolmeaning
-dis directory?
-eexists? (usually we use -f instead)
-fa file?
-gis group id set?
-rreadable?
-snon-zero size?
-uuser id is set?
-wwritable?
-xexecutable?

and use them like this:

Case Statement

Case is called switch case in other languages, and some modern languages call it when case such as Kotlin programming language.

Here is the case statement in bash scripting:

Note that:

[nN] is a way of giving two probabilities small n or capital N.

[yY][eE][sS] is the word yes or YES or any combination of small and capital letters to compose a YeS word.

*) this is the default option in the case statement which is called default in other programming languages.

esac is the closing of the case statement as it is the reversed letters of case . This is the way of ending statements in bash script.

For Loop

Here is a script to rename all text files *.txt at once by a script:

While Loop

Here is a while loop to read nfile.txt line by line.

Bash Script Function Syntax

We created a function to print out Hello, World! and call it to occur!

Bash Script Functions with Parameters

Here is how to write a functions with params in NAMES = "Abanoub Jack John Smith"bash scripting:

The $1 is the first parameter, and $2 is the second parameter. So when we call the function, we should specify the two parameters in the same order like this greet "Abanoub" "25". This means that "Abanoub" is the first param $1 and "25" is the second param $2.

Final Tips

Now, you learned the syntax of bash scripting, but you need to learn the bash commands (I will make a cheatsheet for bash commands later). You will use the bash commands in the bash scripting syntax to build the script you want to run automatically (terminal app).

License

This cheatsheet for Bash Shell Scripting Syntax is created by Abanoub Hanna. You can use this cheatsheet as you like for free. I hope it helps!

Links

Author: Abanoub Hanna

LinkedIn: Abanoub Hanna

Github: Gists & source code apps

YouTube: Abanoub Hanna

Twitter: Abanoub Hanna

تعليقات الفيسبوك
0 تعليقات جوجل

0 تعليقات:

إرسال تعليق