-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshell_scripting.html
177 lines (159 loc) · 10.2 KB
/
shell_scripting.html
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!DOCTYPE HTML>
<!--
Phantom by HTML5 UP
html5up.net | @ajlkn
Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
<head>
<title>Shell Scripting</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="stylesheet" href="assets/css/main.css" />
<noscript><link rel="stylesheet" href="assets/css/noscript.css" /></noscript>
</head>
<body class="is-preload">
<!-- Wrapper -->
<div id="wrapper">
<!-- Header -->
<header id="header">
<div class="inner">
<!-- Logo -->
<a href="index.html" class="logo">
<span class="symbol"><img src="images/NeuroNestLogo.png" alt="NeuroNest Logo" /></span><span class="title">NeuroNest</span>
</a>
<!-- Nav -->
<nav>
<ul>
<li><a href="#menu">Menu</a></li>
</ul>
</nav>
</div>
</header>
<!-- Menu -->
<nav id="menu">
<h2>Menu</h2>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="resource_menu.html">Resources</a></li>
<li><a href="https://sopkoc.wixsite.com/neuronest/forum">Ask a Question</a></li>
<li><a href="https://sopkoc.wixsite.com/neuronest/about">About NeuroNest</a></li>
<li><a href="https://sopkoc.wixsite.com/neuronest/contact">Contact</a></li>
</ul>
</nav>
<!-- Main -->
<div id="main">
<div class="inner">
<h1>Shell Scripting</h1>
<span class="image main"><img src="images/pic13.jpg" alt="" /></span>
<h2>What is shell scripting?</h2>
<p>Shell scripting a way to automate tasks in Unix-like operating systems by writing scripts (sometimes referred to as small programs).</p>
<h3>What is a shell script?</h2>
<p>A shell script is a text file with a list of UNIX commands written in a shell language, such as Bash. Instead of typing commands manually one by one in the terminal, you can save them in a script and run that script to execute all the commands automatically. Shell scripts use plain text with commands written similarly to how you would type them in the terminal. Scripts can use variables to store data and can use structures like loops and conditionals to make decisions and repeat actions. </p>
<h3>Why should I use shell scripting?</h2>
<p>Shell scripting enhances productivity by allowing users to bundle a series of commands into a single, executable file, which can be run with a single command. Using shell scripts can be more efficient, saving you time by running multiple commands with a single script. Shell scripts are also good for consisitency and reproducibility. By using a shell script, you ensure that the commands are run the same way every time. In addition, shell scripts are helpful because they can be re-used to perform routine tasks across different projects or systems.</p>
<h2>Creating and Running a Script</h2>
<ol>
<li>To create a shell script, create a text file with the desired name but use a <code>.sh</code> extention instead of <code>.txt</code>. The <code>.sh</code> is the standard file extension for shell scripts. This extension indicates that the file is a bash script. For example, to create a script named "practice.sh", we would run:</li>
<pre><code>$ nano practice.sh</code></pre>
<li>After opening the .sh file in a text editor (like nano), we need to add <code>#!/bin/bash</code> to the first line. The <code>#!</code> is known as a shebang or a hashbang. The <code>#!</code> tells our computer what system to run the script with (in this case, Bash). The first line in your script should be:</li>
<pre><code>#!/bin/bash</code></pre>
<li>Now, we can add our first command anywhere below the first line. For example, add <code>echo "This is a bash script!"</code>. When the script runs, <code>echo</code> will print <code>This is a bash script!</code>. For example, your script should now look like this:</li>
<pre><code>#!/bin/bash
echo "This is a bash script!"
</code></pre>
<li>Within a script, we can write "comments". Comments are notes in a script that do not get run. A comment is indicated by the <code>#</code> character. For example, we can write a comment adding some information about the purpose of our script:</li>
<pre><code>#!/bin/bash
# This script was made for practice
echo "This is a bash script!"
</code></pre>
<li>Now save the script with <code>Ctrl + O</code> and exit with <code>Ctrl + X</code>.</li>
<li>In order to run the script, we need to specify where our script lives. To do this, we can either specify the absolute path, for example: <code>Users/username/[path]/practice.sh</code>, or the relative path, <code>./practice.sh</code> (remember that <code>.</code> indicates the current working directory) in the command line:</li>
<pre><code>$ ./practice.sh</code></pre>
<pre><code>bash: demo.sh: command not found...</code></pre>
<li>However, you will not be able to run the command above until we give it the correct permissions. To see the file's permissions, run <code>ls -l</code>, which prints detailed information about the contents of the current working directory.</li>
<pre><code>ls -l</code></pre>
<li>The first column of the output shows file permissions. The permissions for practice.sh may look like <code>-rw-r--r--</code>. To change the permissions so the script can be run by all users, run the following code in the command line:</li>
<pre><code>$ chmod +x practice.sh</code></pre>
<li>Re-run <code>ls -l</code>. The permissions for practice.sh should now look like <code>-rwxr-xr-x</code>. Now that we have executable permissions for that file, we can run it:</li>
<pre><code>$ ./practice.sh</code></pre>
<ul>For more information about permissions, please visit the <a href="permissions.html">Permissions</a> page.</ul>
</ol>
<h2>Shell Variables</h2>
<p>See the <a href="bash_data.html">Bash/Command Line</a> page for more information about variables.</p>
<p>Just like in the command line, we can save information under variables within scripts so that we can access the information later. To set a variable, pick a name (containing only letters, numbers, and underscores). For this example, let's use <code>VAR</code>. Note: shell variable names are traditionally capitalized, but do not have to be. Remember, to access a value stored in a variable, we precede the variable with a <code>$</code>. Let's edit our script to add a variable:</p>
<pre><code>$ nano practice.sh</code></pre>
<pre><code>#!/bin/bash
# This script was made for practice
echo "This is a bash script!"
# Declare our new variable
VAR = "I am great at this!"
# Print the variable
echo $VAR2
</code></pre>
<p>In addition to declaring variables within a script, we can also pass arguments into the script from the command line. For example: </p>
<pre><code>$ nano practice.sh "I'm a pro."</code></pre>
<pre><code>#!/bin/bash
# This script was made for practice
echo "This is a bash script!"
# Declare our new variable
VAR = "I am great at this!"
# Print the variable
echo $VAR
# Print variable from command line
echo $1
</code></pre>
<p>In this example, we passed the argument "I'm a pro." into our script. Because "I'm a pro." is our first argument, we will access the variable with <code>$1</code>. If we had two arguments, the second argument would be accessed with <code>$2</code>, and so on. This script should output:</p>
<pre><code>
This is a bash script!
I am great at this!
I'm a pro.
</code></pre>
<ul>
<li>$1, $2, etc., refer to specific arguments (e.g., $1 for the first argument).</li>
<li><code>$@</code> refers to all command-line arguments. </li>
<li>Place variables in quotes if their values might contain spaces to ensure they are correctly interpreted.
</li>
</ul>
<p>Here is an example of a for loop in a script:
<pre><code>$ nano loop.sh</code></pre>
<pre><code>#!/bin/bash
# This is a for loop
for VAR in a b c
do
echo $VAR
done
</code></pre>
The loop runs over once for each item after the word <code>in</code>. Each iteration (i.e., run or loop), the variable $VAR is set to a particular value. The value of <code>$VAR</code> is <code>a</code> in the first iteratation, <code>b</code> in the second iteratation, and <code>c</code> in the third iteratation.</p>
<p>For more infromation, see <a href="https://andysbrainbook.readthedocs.io/en/latest/unix/Unix_07_Scripting.html">Andy's Brain Book Scripting Tutorial</a> for more information and examples.</p>
</div>
</div>
<!-- Footer -->
<footer id="footer">
<div class="inner">
<section>
<h2>Funding</h2>
<p> We would like to express our heartfelt gratitude to <strong>Neurohackademy</strong> at the <strong>University of Washington eScience Institute</strong> for providing invaluable training and support. This experience has significantly enriched our understanding of neuroimaging and data science. We also acknowledge the support of the National Institute of Mental Health (NIMH) grant number <strong>5R25MH112480-08</strong>, which made this opportunity possible.</p>
</section>
<section>
<h2>Follow</h2>
<ul class="icons">
<li><a href="https://x.com/Neuro_Nest" class="icon brands style2 fa-twitter"><span class="label">Twitter</span></a></li>
<li><a href="https://github.com/NeuroHackademy2024/NeuroNest" class="icon brands style2 fa-github"><span class="label">GitHub</span></a></li>
<li><a href="mailto:sopkoc@umich.edu" class="icon solid style2 fa-envelope"><span class="label">Email</span></a></li>
</ul>
</section>
<ul class="copyright">
<li>© Untitled. All rights reserved</li><li>Design: <a href="http://html5up.net">HTML5 UP</a></li>
</ul>
</div>
</footer>
</div>
<!-- Scripts -->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/browser.min.js"></script>
<script src="assets/js/breakpoints.min.js"></script>
<script src="assets/js/util.js"></script>
<script src="assets/js/main.js"></script>
</body>
</html>