Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alexandregama/shell-scripting-essentials
Shell Scripting Essentials
https://github.com/alexandregama/shell-scripting-essentials
Last synced: 23 days ago
JSON representation
Shell Scripting Essentials
- Host: GitHub
- URL: https://github.com/alexandregama/shell-scripting-essentials
- Owner: alexandregama
- Created: 2015-12-26T12:16:17.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-01-02T20:34:36.000Z (almost 9 years ago)
- Last Synced: 2024-04-16T10:52:59.440Z (8 months ago)
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Shell Scripting Essentials
Different shells are available for Unix, Unix-like, or Linux OS
- Bourne shell (sh)
- C shell (csh)
- Korn shell (ksh)
- Bourne again shell (bash)
- Z shell (zsh)We can use a Shebang to indicate and interpreter for execution:
```bash
#! /bin/bash
```To run a shell script, make sure it has execute permission:
```bash
$ chmod u+x your-file
```# The Beginning of the Scripting Journey
#### Hello World in shell
We can use **printf** command as in C language, using the **format** that we need and the **arguments** as follow:
- Using numbers and floating point
```bash
$ printf "%d multiply %f = %f \n" 6 6.0 36.06 mul 6.000000 = 36.000000
```- Using string
```bash
$ printf "%s Scripting \n" ShellShell Scripting
```#### Let's make it scripted
We can create a script file called **math.sh**
```bash
#! /bin/bash
# Filename: math.sh
# Description: echo and printf to format textsecho "Basic Math"
printf "%-7d %-7s %-7.2f =\t%-7.2f\n" 23 plus 5.5 28.5
printf "%-7.2f %-7s %-7d =\t%-7.2f\n" 50.50 minus 20 30.50
printf "%-7d %-7s %-7d =\t%-7d\n" 10 mul 5 50
printf "%-7d %-7s %-7d =\t%-7.2f\n" 27 div 4 6.75
```Will result:
```bash
Basic Math
23 plus 5.50 = 28.50
50.50 minus 20 = 30.50
10 mul 5 = 50
27 div 4 = 6.75
```#### Defining variables of choice
- A variable name can be a combination of alphanumeric and underscore
- Can't start with a number
- Are case sensitive
- Special characters, such as *, -, +, ~, ., ^, and so on, are not used in variable names because they have a special meaning in shellCreating the following file
```bash
#! /bin/bash
# Filename: variables.sh
# Description: Using variablesbook="Learning Spark"
book_description="Learning Spark in an easy way"
book_number_pages=300echo "Learning Spark from book: $book"
echo "Description book: $book_description"
echo "Pages to be read: $book_number_pages"
```Will result
```bash
Learning Spark from book: Learning Spark
Description book: Learning Spark in an easy way
Pages to be read: 300
```