<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://info.stat.cmu.edu/index.php?action=history&amp;feed=atom&amp;title=Bash_files</id>
	<title>Bash files - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://info.stat.cmu.edu/index.php?action=history&amp;feed=atom&amp;title=Bash_files"/>
	<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Bash_files&amp;action=history"/>
	<updated>2026-11-05T03:20:37Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.9</generator>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Bash_files&amp;diff=795&amp;oldid=prev</id>
		<title>Ajshen at 02:52, 29 June 2026</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Bash_files&amp;diff=795&amp;oldid=prev"/>
		<updated>2026-06-29T02:52:17Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 02:52, 29 June 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[Category:Terminal and Command Line]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Bash and &amp;lt;code&amp;gt;.sh&amp;lt;/code&amp;gt; files ==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Bash and &amp;lt;code&amp;gt;.sh&amp;lt;/code&amp;gt; files ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key infowiki:diff:1.41:old-37:rev-795:php=table --&gt;
&lt;/table&gt;</summary>
		<author><name>Ajshen</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Bash_files&amp;diff=37&amp;oldid=prev</id>
		<title>Skipper: Created page with &quot;== Bash and &lt;code&gt;.sh&lt;/code&gt; files ==  &#039;&#039;&#039;This article is still being developed - released earlier to align with Department seminar.&#039;&#039;&#039;  Bash files (often denoted with a post-...&quot;</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Bash_files&amp;diff=37&amp;oldid=prev"/>
		<updated>2024-03-30T00:01:49Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== Bash and &amp;lt;code&amp;gt;.sh&amp;lt;/code&amp;gt; files ==  &amp;#039;&amp;#039;&amp;#039;This article is still being developed - released earlier to align with Department seminar.&amp;#039;&amp;#039;&amp;#039;  Bash files (often denoted with a post-...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Bash and &amp;lt;code&amp;gt;.sh&amp;lt;/code&amp;gt; files ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;This article is still being developed - released earlier to align with Department seminar.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Bash files (often denoted with a post-script &amp;lt;code&amp;gt;.sh&amp;lt;/code&amp;gt; to the file name) can provide a way to store multiple bash commands that can then be run with a single command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;bash my_bash_script.sh&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Some common uses include:&lt;br /&gt;
&lt;br /&gt;
# looping through multiple parameters&lt;br /&gt;
# creating a set of files / folders&lt;br /&gt;
# edit files in minor ways&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt; for-loops ===&lt;br /&gt;
&lt;br /&gt;
Terminal level automatization can occur when you are able to view your work as a set of command-line calls, and often this can entail multiple runs of similar commands - which makes them naturally benefit from using a for-loop. Below is a very simple example of a for-loop that runs a &amp;lt;code&amp;gt;python&amp;lt;/code&amp;gt; script 10 times (it may be useful to read [[Python_on_the_server|Python on the server]]). If we suppose we put the below code in a file called &amp;lt;code&amp;gt;my_bash_script_py.sh&amp;lt;/code&amp;gt; then we could run it with &amp;lt;code&amp;gt;bash my_bash_script_py.sh&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;#!/bin/bash&lt;br /&gt;
for IDX in {1..10}&lt;br /&gt;
  do&lt;br /&gt;
    echo &amp;quot;Step $IDX/10&amp;quot;&lt;br /&gt;
    ipython -i my_file.py $IDX&lt;br /&gt;
  done&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s break down the above code:&lt;br /&gt;
&lt;br /&gt;
# First, the &amp;lt;code&amp;gt;#!/bin/bash&amp;lt;/code&amp;gt; is a comment line with a “bang” (aka &amp;lt;code&amp;gt;#!&amp;lt;/code&amp;gt;) that tells the computer that you will actually be using the &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt; language when you evaluate the file.&lt;br /&gt;
# This for-loop varies the &amp;lt;code&amp;gt;IDX&amp;lt;/code&amp;gt; variable for integers between 1 to 10 (inclusive). To use the &amp;lt;code&amp;gt;IDX&amp;lt;/code&amp;gt; value later we use the notation &amp;lt;code&amp;gt;$IDX&amp;lt;/code&amp;gt; which can be seen in lines 4 and 5 (notice how it can be included inside quotes for the &amp;lt;code&amp;gt;echo&amp;lt;/code&amp;gt; call and outside quotes for the &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; call). The 5th line takes in &amp;lt;code&amp;gt;$IDX&amp;lt;/code&amp;gt; as the first parameter for the python file &amp;lt;code&amp;gt;my_file.py&amp;lt;/code&amp;gt; (see [[Python_on_the_server|Python on the server]] and [[R_on_the_server|R on the server ]] for more details about passing arguments in from the command line).&lt;br /&gt;
&lt;br /&gt;
==== Defining more complex &amp;lt;code&amp;gt;bash&amp;lt;/code&amp;gt; for-loops ====&lt;br /&gt;
&lt;br /&gt;
The above for loop is very simple and constrained about what values you are going through. The below table shows some examples of more complex calls&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!width=&amp;quot;31%&amp;quot;| code&lt;br /&gt;
!width=&amp;quot;68%&amp;quot;| description&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;for VALUE in 100 200 400 ...&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;$VALUE&amp;lt;/code&amp;gt; takes the value &amp;lt;code&amp;gt;100&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;200&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;400&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;for VARIABLE in &amp;lt;code&amp;gt;text1 text3 text7 ...&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;$VARIABLE&amp;lt;/code&amp;gt; takes the &amp;lt;code&amp;gt;text1&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;text3&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;text7&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;...&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;for idx in {0..10..2} ...&amp;lt;/code&amp;gt; |&amp;lt;code&amp;gt;$idx&amp;lt;/code&amp;gt;takes the values &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;2&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;4&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;6&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;8&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;10&amp;lt;/code&amp;gt; | &lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;EXTERNAL_NUM=10 &lt;br /&gt;
for i in $seq(1 2 $EXTERNAL_NUM) ...&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
| &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;takes odd values between 1 and&amp;lt;code&amp;gt;$EXTERNAL_NUM&amp;lt;/code&amp;gt;(inclusive). Where we defined the&amp;lt;code&amp;gt;EXTERNAL_NUM&amp;lt;/code&amp;gt; right before&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
For more examples of for-loops in bash see [https://www.cyberciti.biz/faq/bash-for-loop/ cyberciti.biz]&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
Below we provide two examples (from Ben LeRoy and Mike Stanley respectively). These try to capture the complexities you can do with bash scripts.&lt;br /&gt;
&lt;br /&gt;
=== Example 1: Parallelization ===&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Warning: below shows how to parallelize inside a bash script. This can be dangerous since it sends processes to the background so it’s harder/impossible to cancel. This can be very bad as your processes can use too much RAM and crash your computer…&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Script’s goal:&amp;#039;&amp;#039;&amp;#039; to process 60 jobs (with parameters &amp;lt;code&amp;gt;$BIDX&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$INDEX&amp;lt;/code&amp;gt;) with some parallelization (maximum 6 jobs run at once) on a local computer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;#!/bin/bash&lt;br /&gt;
&lt;br /&gt;
N=6 # maximum number of jobs run at once&lt;br /&gt;
&lt;br /&gt;
for BIDX in {1..6}&lt;br /&gt;
  do&lt;br /&gt;
    echo &amp;quot;--&amp;quot;&lt;br /&gt;
    echo &amp;quot;Number of Sims Idx: $BIDX&amp;quot;&lt;br /&gt;
    echo &amp;quot;--&amp;quot;&lt;br /&gt;
    for INDEX in {1..10}&lt;br /&gt;
      do&lt;br /&gt;
        Rscript 2-assess_sensitivity.R $BIDX $INDEX default default &amp;amp;&lt;br /&gt;
        if [[ $(jobs -r -p | wc -l) -ge $N ]]; then&lt;br /&gt;
          wait&lt;br /&gt;
        fi&lt;br /&gt;
    done&lt;br /&gt;
    wait&lt;br /&gt;
  done&lt;br /&gt;
echo &amp;quot;all completed&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Code breakdown:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
# [Line 3] We set &amp;lt;code&amp;gt;N=6&amp;lt;/code&amp;gt; to capture a global variable (&amp;lt;code&amp;gt;$N&amp;lt;/code&amp;gt;) is the total number of jobs that can be used by this script. This constraint with be identified below. Since this relates to the number of jobs run at the same time, one should be aware about the number of cores your computer has and the amount of RAM you use for each job (the later we’ll cause memory overload if you’re not careful).&lt;br /&gt;
# [Line 5:9] Our first for-loop goes across &amp;lt;code&amp;gt;$BIDX&amp;lt;/code&amp;gt; integers between 1 and 6 (making some printouts as it does so).&lt;br /&gt;
# [Line 10:11] We have an inner for-loop with &amp;lt;code&amp;gt;$INDEX&amp;lt;/code&amp;gt; taking integer values between 1 and 10.&lt;br /&gt;
# [Line 12] For each of the inner for-loops, we run an &amp;lt;code&amp;gt;.R&amp;lt;/code&amp;gt; script that takes in 4 parameters (&amp;lt;code&amp;gt;$BIDX&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;$INDEX&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;default&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;default&amp;lt;/code&amp;gt; – see [[R_on_the_server]] for more details). The &amp;lt;code&amp;gt;&amp;amp;amp;&amp;lt;/code&amp;gt; at the end of the line sends the code to be run in the background.&lt;br /&gt;
# [Line 13:15] Inside the inner for-loop and after the &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt; code is run, we have an &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; statement (with the ending of the if statement denoted &amp;lt;code&amp;gt;fi&amp;lt;/code&amp;gt;). The logic of the &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; statement (inside the &amp;lt;code&amp;gt;[[ ]]&amp;lt;/code&amp;gt;) is started with the &amp;lt;code&amp;gt;$&amp;lt;/code&amp;gt;-sign, and runs the command &amp;lt;code&amp;gt;jobs -r -p&amp;lt;/code&amp;gt; (which creates a list of the jobs running in the background) and then pipes that into &amp;lt;code&amp;gt;wc -l&amp;lt;/code&amp;gt; (which counts) the total number of these jobs running. This value is then compared (&amp;lt;code&amp;gt;-ge $N&amp;lt;/code&amp;gt;) with N, and if greater or equal to &amp;lt;code&amp;gt;$N&amp;lt;/code&amp;gt;, then the if statement is executed.&lt;br /&gt;
# [Line 14] If the &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; statement is executed then it executes the function &amp;lt;code&amp;gt;wait&amp;lt;/code&amp;gt; which just waits until the jobs sent to the background (with the &amp;lt;code&amp;gt;&amp;amp;amp;&amp;lt;/code&amp;gt;) are finished before proceeding.&lt;br /&gt;
# [Line 16] The &amp;lt;code&amp;gt;done&amp;lt;/code&amp;gt; statement completes the inner for-loop&lt;br /&gt;
# [Line 17] Another &amp;lt;code&amp;gt;wait&amp;lt;/code&amp;gt; call makes sure that all of those jobs sent to the background for the inner for-loop are done before proceeding&lt;br /&gt;
# [Line 18] The &amp;lt;code&amp;gt;done&amp;lt;/code&amp;gt; statement completes the outer for-loop&lt;br /&gt;
# [Line 19] Prints out the message “all complete”. Given the second &amp;lt;code&amp;gt;wait&amp;lt;/code&amp;gt; on line 17, this will occur only after all &amp;lt;code&amp;gt;BIDX&amp;lt;/code&amp;gt; values are completely processed.&lt;br /&gt;
&lt;br /&gt;
More information on parallelizing with bash can be found here: [https://www.cyberciti.biz/faq/how-to-run-command-or-code-in-parallel-in-bash-shell-under-linux-or-unix/ cyberciti].&lt;br /&gt;
&lt;br /&gt;
=== Example 2: complex file creation, rewriting and changing preferences ===&lt;br /&gt;
&lt;br /&gt;
The following code is complex so we will be breaking it into 4 parts (relative to the 4 for-loops it contains.) Please image all these steps were in a single bash file. Below is the header that was in the original file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;#!/bin/bash&lt;br /&gt;
#&lt;br /&gt;
# Script to set up a multijob directory&lt;br /&gt;
#&lt;br /&gt;
# 1. Creates one cmdfile for every 4 jobs (list of file paths to the element bash scripts)&lt;br /&gt;
# 2. Creates each bash script in the above file&lt;br /&gt;
# 3. Creates directory for each element in the above&lt;br /&gt;
# 4. Creates PBS script for each cmdfile&lt;br /&gt;
#&lt;br /&gt;
# INPUTS&lt;br /&gt;
# 1. Target directory where files and directories can be made&lt;br /&gt;
# 2. Template element bash script&lt;br /&gt;
# 3. Pre-compiled GEOS-Chem directory&lt;br /&gt;
#&lt;br /&gt;
# NOTE:&lt;br /&gt;
# - the sample element bash script is expected to be in the same directory&lt;br /&gt;
#   as this script&lt;br /&gt;
#&lt;br /&gt;
# STEPS FOR USE&lt;br /&gt;
# 1. set directory where multijob will be executed (BASE_DIR)&lt;br /&gt;
# 2. set directory where compiled code lives (COMPILED_CODE_DIR)&lt;br /&gt;
# 3. define number of ensemble elements (NUM_ELEMENTS)&lt;br /&gt;
#&lt;br /&gt;
# Author        : Mike Stanley&lt;br /&gt;
# Created       : October 27, 2020&lt;br /&gt;
# Last Modified : November 24, 2020&lt;br /&gt;
#==============================================================================&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Step 1 ====&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Step’s goal:&amp;#039;&amp;#039;&amp;#039; The goal of the code in this step is to (1) first define a set of global variables (some focused on the overall location of files) and then (2) create a set of files in a set of subfolders&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;# operational params&lt;br /&gt;
BASE_DIR=/glade/work/mcstanley/monte_carlo/multijob&lt;br /&gt;
COMPILED_CODE_DIR=/glade/u/home/mcstanley/gc_adj_runs/monte_carlo_element_const_start&lt;br /&gt;
NUM_ELEMENTS=20&lt;br /&gt;
&lt;br /&gt;
# stems&lt;br /&gt;
SCRIPT_STEM=element&lt;br /&gt;
DIR_STEP=ens&lt;br /&gt;
LAUNCH_STEM=launch_file&lt;br /&gt;
&lt;br /&gt;
# create and fill the cmdfile&lt;br /&gt;
CMDFILE_NUM=0&lt;br /&gt;
FILE_NUM=0&lt;br /&gt;
for i in $(seq 0 $( expr ${NUM_ELEMENTS} - 1 ))&lt;br /&gt;
do&lt;br /&gt;
    echo $BASE_DIR/${SCRIPT_STEM}_${i}.sh &amp;gt;&amp;gt; $BASE_DIR/cmdfile_$FILE_NUM&lt;br /&gt;
&lt;br /&gt;
    # update indices&lt;br /&gt;
    CMDFILE_NUM=$((CMDFILE_NUM + 1))&lt;br /&gt;
&lt;br /&gt;
    if [ $((CMDFILE_NUM % 4)) -eq 0 ]; then&lt;br /&gt;
        FILE_NUM=$((FILE_NUM + 1))&lt;br /&gt;
    fi&lt;br /&gt;
&lt;br /&gt;
done&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Code breakdown:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
# [Line 2:4, 7:9] define global variables&lt;br /&gt;
# [Line 12:13] define global variables for the for-loop. The first is just an index of the for loop (@Mike - why not just use &amp;lt;code&amp;gt;$i&amp;lt;/code&amp;gt;?). The second will be updated every 4 files are created and helps define new folders.&lt;br /&gt;
# [Line 14:15] starts a for-loop that will see &amp;lt;code&amp;gt;$i&amp;lt;/code&amp;gt; go from 0 to 19 (inclusively), given that on line 4 we defined &amp;lt;code&amp;gt;$NUM_ELEMENTS=20&amp;lt;/code&amp;gt;.&lt;br /&gt;
# [Line 16] The function call &amp;lt;code&amp;gt;echo _1_ &amp;amp;gt;&amp;amp;gt; _2_&amp;lt;/code&amp;gt; adds the &amp;lt;code&amp;gt;_1_&amp;lt;/code&amp;gt; as the final text line of the file &amp;lt;code&amp;gt;_2_&amp;lt;/code&amp;gt; (it also creates &amp;lt;code&amp;gt;_2_&amp;lt;/code&amp;gt; if it doesn’t already exist). &amp;lt;code&amp;gt;_1_&amp;lt;/code&amp;gt; is the pasting of the &amp;lt;code&amp;gt;$BASE_DIR&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;/&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;$SCRIPT_STEM&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;_&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;$i&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.sh&amp;lt;/code&amp;gt; (as similar thing is true for &amp;lt;code&amp;gt;_2_&amp;lt;/code&amp;gt;.&lt;br /&gt;
# [Line 18] (@Mike - again I’m not sure why this included). This line just updates the &amp;lt;code&amp;gt;$CMDFILE_NUM&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; plus it’s original value&lt;br /&gt;
# [Line 20:22] This is an &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; statement that checks if the variable &amp;lt;code&amp;gt;$CMDFILE_NUM&amp;lt;/code&amp;gt;’s remainder after defining by 4 (&amp;lt;code&amp;gt;mod&amp;lt;/code&amp;gt; = &amp;lt;code&amp;gt;%&amp;lt;/code&amp;gt;) equals (&amp;lt;code&amp;gt;-eq&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;. If so it then updates the &amp;lt;code&amp;gt;$FILE_NUM&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; plus it’s original value. Ends the &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; statement with &amp;lt;code&amp;gt;fi&amp;lt;/code&amp;gt;&lt;br /&gt;
# [Line 24] ends the for loop from line 12&lt;br /&gt;
&lt;br /&gt;
==== Step 2 ====&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Step’s goal:&amp;#039;&amp;#039;&amp;#039; create additional bash files (associated with numbering above) and edit those bash files to have &amp;lt;code&amp;gt;ENS_NUM&amp;lt;/code&amp;gt; also associated with the new name of the file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;# copy sample bash script and alter element number&lt;br /&gt;
for i in $(seq 0 $( expr ${NUM_ELEMENTS} - 1 ))&lt;br /&gt;
do&lt;br /&gt;
    # create the copy for element i&lt;br /&gt;
    cp ./sample_element.sh $BASE_DIR/${SCRIPT_STEM}_${i}.sh&lt;br /&gt;
&lt;br /&gt;
    # alter the ENS_NUM= line&lt;br /&gt;
    # NOTE: there might be platform dependence with the sed command&lt;br /&gt;
    replace_str=&amp;quot;ENS_NUM=${i}&amp;quot;&lt;br /&gt;
    sed -i.bu &amp;quot;s/ENS_NUM=0/${replace_str}/&amp;quot; $BASE_DIR/${SCRIPT_STEM}_${i}.sh&lt;br /&gt;
&lt;br /&gt;
    # remove the created backup&lt;br /&gt;
    rm $BASE_DIR/${SCRIPT_STEM}_${i}.sh.bu&lt;br /&gt;
&lt;br /&gt;
done&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Code breakdown:&amp;#039;&amp;#039;&amp;#039; &lt;br /&gt;
# [Line 2:3, 15] see lines 14:15 and 24 in step 1. &lt;br /&gt;
# [Line 5] make a copy of &amp;lt;code&amp;gt;./sample_element.sh&amp;lt;/code&amp;gt; with name and in the directory &amp;lt;code&amp;gt;$BASE_DIR&amp;lt;/code&amp;gt; with name &amp;lt;code&amp;gt;$SCRIPT_STEM_${i}.sh&amp;lt;/code&amp;gt; (so for the first for-loop step, we’d have it be called &amp;lt;code&amp;gt;element_0.sh&amp;lt;/code&amp;gt; (remember &amp;lt;code&amp;gt;$BASE_DIR&amp;lt;/code&amp;gt; was defined in step 1’s line 2). &lt;br /&gt;
# [Line 9] define a new variable &amp;lt;code&amp;gt;replacement_string&amp;lt;/code&amp;gt; &lt;br /&gt;
# [Line 10] use the &amp;lt;code&amp;gt;sed&amp;lt;/code&amp;gt; command to relace all strings of the form &amp;lt;code&amp;gt;ENS_NUM=0&amp;lt;/code&amp;gt; with your &amp;lt;code&amp;gt;$replace_str&amp;lt;/code&amp;gt; in the newly created fill (from line 5). See [https://www.geeksforgeeks.org/sed-command-in-linux-unix-with-examples/ geeksforgeeks] for more details on the power of &amp;lt;code&amp;gt;sed&amp;lt;/code&amp;gt;. &lt;br /&gt;
# [Line 11] Since we have to extra parameter &amp;lt;code&amp;gt;sed -i.bu&amp;lt;/code&amp;gt; in line 10, we delete a back-up file created from &amp;lt;code&amp;gt;sed&amp;lt;/code&amp;gt; (@Mike - I’m not 100% sure this is true - and is it needed?)&lt;br /&gt;
&lt;br /&gt;
==== Step 3 ====&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Step’s goal:&amp;#039;&amp;#039;&amp;#039; change file privileges of script files and create and fill new directories associated with new scripts (from step 2)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;# give executable privileges to the above scripts&lt;br /&gt;
chmod ugo+x $BASE_DIR/${SCRIPT_STEM}_*&lt;br /&gt;
&lt;br /&gt;
# create the code directories for each element&lt;br /&gt;
for i in $(seq 0 $( expr ${NUM_ELEMENTS} - 1 ))&lt;br /&gt;
do&lt;br /&gt;
    # make the directory&lt;br /&gt;
    mkdir $BASE_DIR/ens_${i}&lt;br /&gt;
&lt;br /&gt;
    # make the directory by copying over contents&lt;br /&gt;
    cp -r $COMPILED_CODE_DIR/* $BASE_DIR/ens_${i}&lt;br /&gt;
&lt;br /&gt;
done&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Code breakdown:&amp;#039;&amp;#039;&amp;#039; &lt;br /&gt;
#[Line 2] change file privileges for all the new script files using &amp;lt;code&amp;gt;chmod&amp;lt;/code&amp;gt;. Note that &amp;lt;code&amp;gt;chmod ugo+x&amp;lt;/code&amp;gt; updates the e&amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;ecutability privileges of the file to the &amp;lt;code&amp;gt;u&amp;lt;/code&amp;gt;ser that owns it, members in the user’s &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;roup and those &amp;lt;code&amp;gt;o&amp;lt;/code&amp;gt;utside the user’s group. See [https://www.computerhope.com/unix/uchmod.htm computerhope] for more references. This was necessary due to default file privileges on the server and how user structure was setup. &lt;br /&gt;
# [Line 5:6] start of a for-loop for variable &amp;lt;code&amp;gt;$i&amp;lt;/code&amp;gt; ranging between &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;$NUM_ELMENTS - 1&amp;lt;/code&amp;gt; - aka for all newly make files in Step 2. &lt;br /&gt;
# [Line 8] make new directories assocated with each new script (@Mike: should this be &amp;lt;code&amp;gt;mkdir ${BASE_DIR}/${DIR_STEP}_${i}&amp;lt;/code&amp;gt;? ) &lt;br /&gt;
# [Line 11] copy the full directory (&amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt;: recursively) of the &amp;lt;code&amp;gt;$COMPILED_CODE_DIR/&amp;lt;/code&amp;gt; into our newly make folders&lt;br /&gt;
&lt;br /&gt;
==== Step 4 ====&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Step’s goal:&amp;#039;&amp;#039;&amp;#039; copy a launch script into a set of new folders, then update the copied script by changing the name of the cmdfile it is using. (@Mike: is the LAUNCH_STEP folders already created?)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;# create PBS run files for each cmdfile&lt;br /&gt;
CMD_FILE_NMS=$BASE_DIR/cmdfile*&lt;br /&gt;
&lt;br /&gt;
COUNT=0&lt;br /&gt;
for file in $CMD_FILE_NMS&lt;br /&gt;
do&lt;br /&gt;
    # create the copy for cmdfile i&lt;br /&gt;
    cp ./sample_launch_script $BASE_DIR/${LAUNCH_STEM}_${COUNT}&lt;br /&gt;
&lt;br /&gt;
    # replace the cmdfile name&lt;br /&gt;
    replace_str=&amp;quot;cmdfile_${COUNT}&amp;quot;&lt;br /&gt;
    sed -i.bu &amp;quot;s/\/cmdfile/\/${replace_str}/&amp;quot; $BASE_DIR/${LAUNCH_STEM}_${COUNT}&lt;br /&gt;
&lt;br /&gt;
    # remove the created backup&lt;br /&gt;
    rm $BASE_DIR/${LAUNCH_STEM}_${COUNT}.bu&lt;br /&gt;
&lt;br /&gt;
    COUNT=$((COUNT + 1))&lt;br /&gt;
done&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Code breakdown:&amp;#039;&amp;#039;&amp;#039; &lt;br /&gt;
# [Line 2] define a list of variable names starting with a certain structure &lt;br /&gt;
# [Line 4, 17] define a variable that starts at &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; and add &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; to it each time the following loop runs &lt;br /&gt;
# [Line 5:6, 14] a for-loop across the file names in the list defined in line 2. &lt;br /&gt;
# [Line 8] copy file &amp;lt;code&amp;gt;./sample_launch_script&amp;lt;/code&amp;gt; into file into new older &lt;br /&gt;
# [Line 10:11, 13] replace the string &amp;lt;code&amp;gt;/cmdfile&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;/cmdfile_[i]&amp;lt;/code&amp;gt; where i in a new index (in the file you just created with the copy in line 8). Line 13 removes temporary file creates with the replacement&lt;/div&gt;</summary>
		<author><name>Skipper</name></author>
	</entry>
</feed>