-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShell Scripting Basics.txt
More file actions
91 lines (57 loc) · 1.73 KB
/
Shell Scripting Basics.txt
File metadata and controls
91 lines (57 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Shell Scripting Basics
bash_profile vs bashrc
------------------------------
Checking Your Environment
-----------------------------------
env - prints all exported (globally available) variables
set - prints all locally scoped variables
simple-script.sh
# creates a subshell which is separate from the parent shell from which it was launched
#!/bin/bash
# built-in shell util that says breakout of the shell when a failure/error is raised (exit status not 0)
set -e
echo 'Hello World' // executes this command and exits
Exit Status
---------------
0 - successful
1 - failure/error
> ls
> echo $? // displays last exit status
Escaping Characters
------------------------------
Using /dev/null
---------------------
- generally the blackhole of a system
- when any file/output is sent here it is gone forever
ls -al >> /dev/null
# no output will be shown
Reading Input
--------------------
echo "Enter your First Name: "
read FIRSTNAME
echo " Your first name is: $FIRSTNAME"
Accepting Arguments
---------------------------
- arguments are positional by nature
- you can take each argument (in the order they were passed in) and assign them to variables
#!/bin/bash
USERNAME=$1 // argument 1
PASSWORD=$2 // argument 2
echo "The following username is $USERNAME and $PASSWORD
$ args.sh Lowell Password1
The following Username is Lowell and Password is Password1
Flow Control
-----------------
echo "Guess a number between 1 and 5: "
read GUESS
if [ $GUESS -eq 3 ]
then
echo "You Guessed the Correct Number!"
fi
========
FILENAME=$1
echo "Testing for the existence of a file called $FILENAME"
# -a, -e, -f all check for existence (true or false) of something
if [ -e $FILENAME ]
then
echo "File $FILENAME does exist!"