Shell Script
\#linux
To create a shell script, use the following steps:
Create a file with a text editor containing the commands to execute
Place on the first line:
#!/bin/bashUse the
chmodcommand to make the file executable:chmod u+x <file>Lets you run the script with ./script
Shebang
Tells the kernel what shell interpreter to use to run the script
Must be on the first line
#!/bin/bash#!/usr/bin/ruby
Variable
Defining and accessing local variable
Accessing environment variable
Defining environment variable
Deleting environment variable
Special Variables
$0, $1, ..., $9, ${10}, ${11}, ...are the arguments to the script$#means number of command line variables$?is the exit code of the last command to be run0means command executed successfullyother number means false
Conditional Statement
if, elif, else
We can use test command to create the boolean expression The test command can also be written using two square brackets: []
String comparisons
Operator | Description | Example |
|---|---|---|
| True iff length of string is zero |
|
| True iff length of string is not zero |
|
| True iff strings are equal |
|
| True iff strings are not equal |
|
| True iff |
|
| True iff |
|
Notice that
File / Directory checking
Operator | Description | Example |
|---|---|---|
| True iff file exists |
|
| True iff file size > 0 |
|
| True iff file is a directory |
|
| True iff file is a plain file |
|
| True iff file has read permission for current user |
|
| True iff file has write permission for current user |
|
| True iff file has execute permission for current user |
|
Number comparison
Operator | Description | Example |
|---|---|---|
| True iff integers are equal |
|
| True iff integers are not equal |
|
| True iff first integer is greater than second integer |
|
| True iff first integer is greater than or equal to second integer |
|
| True iff first integer is less than second integer |
|
| True iff first integer is less than or equal to second integer |
|
case (c++ switch)
While Statement
For Statement
We can use
seqstatement to create the iterable listseq <start_value> <increment_value> <end_value>
We can use
{ .. .. }statement to create the iterable list{<start_value>..<end_value>..<increment_value>}
e.g.
e.g.
e.g.
Function
create function
calling function