KEMBAR78
Bash Scripting Crash Course | PDF | Software Development | System Software
0% found this document useful (0 votes)
83 views1 page

Bash Scripting Crash Course

Uploaded by

mh3602058
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views1 page

Bash Scripting Crash Course

Uploaded by

mh3602058
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

BASH SCRIPTING BASICS

script.sh
1 ^!/bin/bash Shebang Line
2
3 username="Jay" Variables
4 filename=$3
5
6 read -p "Enter your username: " user User Input
7 echo "Username: $user"
8
9 if [ "$EUID" -ne 0 ]; then
10 echo "You are not running this script as the root user."
11 else Conditional if Statement
12 echo "You are running this script as the root user."
13 fi
14
15 echo "Counting to 5:"
16 for i in {1^.5}; do
17 echo "$i" For Loop
18 done
19
20
21 function greet() {
22 echo "Hello, $1!" Functions
23 }
24 greet "Alice"
25
26 echo "Enter a number between 1 and 2: " bash script.sh
27 read num
28 case $num in
29 1) echo "You chose one." ^; Conditional Case Statement
30 2) echo "You chose two." ^;
31 *) echo "Invalid choice." ^;
32 esac
33
34 if [ -e "$filename" ] ^& [ -d "$filename" ]; then
35 echo "File exists and is a directory."
36 else File Operations
37 echo "File does not exist or is not a directory."
38 fi
39
40 echo "First argument: $1" Command Line Arguments
41 echo "Second argument: $2"
42
43 cat nonexistent-file.txt 2> /dev/null Exit Status Codes
44 echo "Exit status: $?"
45
46 fruits=("Apple" "Orange" "Banana")
Indexed Arrays
47 echo "Fruits: ${fruits[0]}"
48
49 declare -A capitals
50 capitals[USA]="Washington D.C."
51 capitals[France]="Paris" Associative Arrays
52 echo "Capital of France: ${capitals[France]}"
53
54 current_date=$(date)
Command Substitution
55 echo "Today's date is: $current_date"
56
57 echo "This is a sample text." > example.txt Command Line Redirections
58 find / -name hello.txt &> /dev/null
59
60 result=$( expr 15 - 2 ) Arithmetic Operations
61 echo $result
62
63 SRC="/path/to/foo.cpp"
64 BASEPATH=${SRC^#^/} Parameter Expansion
65 echo $BASEPATH
66
67 trap 'echo "Received SIGTERM signal. Cleaning up^^."; exit' SIGTERM Process Signal Handling
68
69 # This is a single line comment
70
Comments
71 :’ this a multiline
72 commment‘

sysxplore.com

You might also like