|
| 1 | +<!DOCTYPE html> |
| 2 | +<html class="writer-html5" lang="en" > |
| 3 | +<head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 7 | + <link rel="shortcut icon" href="../img/favicon.ico" /> |
| 8 | + <title>Advanced Command Line Features SOP - Vets Who Code SOP's</title> |
| 9 | + <link rel="stylesheet" href="../css/theme.css" /> |
| 10 | + <link rel="stylesheet" href="../css/theme_extra.css" /> |
| 11 | + <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css" /> |
| 12 | + <link href="../css/custom.css" rel="stylesheet" /> |
| 13 | + |
| 14 | + <script> |
| 15 | + // Current page data |
| 16 | + var mkdocs_page_name = "Advanced Command Line Features SOP"; |
| 17 | + var mkdocs_page_input_path = "advanced-command-line-features.md"; |
| 18 | + var mkdocs_page_url = null; |
| 19 | + </script> |
| 20 | + |
| 21 | + <!--[if lt IE 9]> |
| 22 | + <script src="../js/html5shiv.min.js"></script> |
| 23 | + <![endif]--> |
| 24 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script> |
| 25 | + <script>hljs.highlightAll();</script> |
| 26 | +</head> |
| 27 | + |
| 28 | +<body class="wy-body-for-nav" role="document"> |
| 29 | + |
| 30 | + <div class="wy-grid-for-nav"> |
| 31 | + <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav"> |
| 32 | + <div class="wy-side-scroll"> |
| 33 | + <div class="wy-side-nav-search"> |
| 34 | + <a href=".." class="icon icon-home"> Vets Who Code SOP's |
| 35 | + </a><div role="search"> |
| 36 | + <form id ="rtd-search-form" class="wy-form" action="../search.html" method="get"> |
| 37 | + <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" title="Type search term here" /> |
| 38 | + </form> |
| 39 | +</div> |
| 40 | + </div> |
| 41 | + |
| 42 | + <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu"> |
| 43 | + <ul> |
| 44 | + <li class="toctree-l1"><a class="reference internal" href="..">Home</a> |
| 45 | + </li> |
| 46 | + </ul> |
| 47 | + </div> |
| 48 | + </div> |
| 49 | + </nav> |
| 50 | + |
| 51 | + <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"> |
| 52 | + <nav class="wy-nav-top" role="navigation" aria-label="Mobile navigation menu"> |
| 53 | + <i data-toggle="wy-nav-top" class="fa fa-bars"></i> |
| 54 | + <a href="..">Vets Who Code SOP's</a> |
| 55 | + |
| 56 | + </nav> |
| 57 | + <div class="wy-nav-content"> |
| 58 | + <div class="rst-content"><div role="navigation" aria-label="breadcrumbs navigation"> |
| 59 | + <ul class="wy-breadcrumbs"> |
| 60 | + <li><a href=".." class="icon icon-home" aria-label="Docs"></a></li> |
| 61 | + <li class="breadcrumb-item active">Advanced Command Line Features SOP</li> |
| 62 | + <li class="wy-breadcrumbs-aside"> |
| 63 | + </li> |
| 64 | + </ul> |
| 65 | + <hr/> |
| 66 | +</div> |
| 67 | + <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article"> |
| 68 | + <div class="section" itemprop="articleBody"> |
| 69 | + |
| 70 | + <h1 id="advanced-command-line-features-sop">Advanced Command Line Features SOP</h1> |
| 71 | +<h2 id="1-introduction">1. Introduction</h2> |
| 72 | +<p>This Standard Operating Procedure (SOP) outlines advanced command line features and techniques that enhance productivity and efficiency for software engineers, writers, instructors, and other tech professionals. These advanced features include shell scripting basics, environmental variables, aliases and shortcuts, command history manipulation, job control, and task scheduling.</p> |
| 73 | +<h2 id="2-shell-scripting-basics">2. Shell Scripting Basics</h2> |
| 74 | +<p>Shell scripting allows for automation of complex tasks and repetitive operations.</p> |
| 75 | +<h3 id="21-creating-a-shell-script">2.1 Creating a Shell Script</h3> |
| 76 | +<ol> |
| 77 | +<li>Create a new file with a <code>.sh</code> extension.</li> |
| 78 | +<li>Add the shebang line at the top: <code>#!/bin/bash</code></li> |
| 79 | +<li>Make the script executable: <code>chmod +x script.sh</code></li> |
| 80 | +</ol> |
| 81 | +<h3 id="22-syntax">2.2 Syntax</h3> |
| 82 | +<h4 id="variables">Variables</h4> |
| 83 | +<pre><code class="language-bash">variable_name=value |
| 84 | +my_var="Hello, World" |
| 85 | +</code></pre> |
| 86 | +<h4 id="conditional-statements">Conditional Statements</h4> |
| 87 | +<pre><code class="language-bash">if [ "$variable_one" -gt "$variable_two" ]; then |
| 88 | + echo "Variable one is greater" |
| 89 | +elif [ "$variable_one" -eq "$variable_two" ]; then |
| 90 | + echo "Variables are equal" |
| 91 | +else |
| 92 | + echo "Variable two is greater" |
| 93 | +fi |
| 94 | +</code></pre> |
| 95 | +<h4 id="loops">Loops</h4> |
| 96 | +<p>For Loop:</p> |
| 97 | +<pre><code class="language-bash">for i in {1..10}; do |
| 98 | + echo $i |
| 99 | +done |
| 100 | +</code></pre> |
| 101 | +<p>While Loop:</p> |
| 102 | +<pre><code class="language-bash">count=0 |
| 103 | +while [ $count -lt 10 ]; do |
| 104 | + echo $count |
| 105 | + ((count++)) |
| 106 | +done |
| 107 | +</code></pre> |
| 108 | +<h3 id="23-structure">2.3 Structure</h3> |
| 109 | +<h4 id="shebang">Shebang</h4> |
| 110 | +<pre><code class="language-bash">#!/bin/bash |
| 111 | +</code></pre> |
| 112 | +<h4 id="functions">Functions</h4> |
| 113 | +<pre><code class="language-bash">my_function() { |
| 114 | + echo "Hello from my_function!" |
| 115 | +} |
| 116 | + |
| 117 | +# Call the function |
| 118 | +my_function |
| 119 | +</code></pre> |
| 120 | +<h3 id="24-best-practices">2.4 Best Practices</h3> |
| 121 | +<ul> |
| 122 | +<li>Use consistent indentation (2 or 4 spaces).</li> |
| 123 | +<li>Comment your code for clarity.</li> |
| 124 | +<li>Handle errors and check return codes.</li> |
| 125 | +</ul> |
| 126 | +<h3 id="25-debugging">2.5 Debugging</h3> |
| 127 | +<p>Basic debugging:</p> |
| 128 | +<pre><code class="language-bash">bash -x script.sh |
| 129 | +</code></pre> |
| 130 | +<p>Advanced debugging:</p> |
| 131 | +<pre><code class="language-bash">set -e # Exit on first error |
| 132 | +set -u # Treat unset variables as errors |
| 133 | +set +e # Continue even if there is an error |
| 134 | +</code></pre> |
| 135 | +<h2 id="3-environmental-variables">3. Environmental Variables</h2> |
| 136 | +<p>Environmental variables store system-wide or user-specific configuration information.</p> |
| 137 | +<h3 id="31-setting-variables">3.1 Setting Variables</h3> |
| 138 | +<pre><code class="language-bash"># Temporary (session only) |
| 139 | +VARIABLE_NAME=value |
| 140 | + |
| 141 | +# Permanent (add to ~/.bashrc or ~/.bash_profile) |
| 142 | +echo 'export PERMANENT_VAR="I'm here to stay!"' >> ~/.bashrc |
| 143 | +source ~/.bashrc |
| 144 | +</code></pre> |
| 145 | +<h3 id="32-retrieving-variables">3.2 Retrieving Variables</h3> |
| 146 | +<pre><code class="language-bash">echo $VARIABLE_NAME |
| 147 | +env # List all environment variables |
| 148 | +</code></pre> |
| 149 | +<h3 id="33-exporting-variables">3.3 Exporting Variables</h3> |
| 150 | +<pre><code class="language-bash">export VARIABLE_NAME |
| 151 | +# Or in one line |
| 152 | +export VARIABLE_NAME=value |
| 153 | +</code></pre> |
| 154 | +<h3 id="34-unsetting-variables">3.4 Unsetting Variables</h3> |
| 155 | +<pre><code class="language-bash">unset VARIABLE_NAME |
| 156 | +</code></pre> |
| 157 | +<h2 id="4-aliases-and-shortcuts">4. Aliases and Shortcuts</h2> |
| 158 | +<p>Aliases allow you to create custom shortcuts for frequently used commands.</p> |
| 159 | +<h3 id="41-creating-aliases">4.1 Creating Aliases</h3> |
| 160 | +<pre><code class="language-bash"># Temporary (session only) |
| 161 | +alias myalias='my long command here' |
| 162 | + |
| 163 | +# Permanent (add to ~/.bashrc or ~/.zshrc) |
| 164 | +echo "alias persist='I will survive reboots!'" >> ~/.bashrc |
| 165 | +source ~/.bashrc |
| 166 | +</code></pre> |
| 167 | +<h3 id="42-common-aliases">4.2 Common Aliases</h3> |
| 168 | +<pre><code class="language-bash">alias ll='ls -l' |
| 169 | +alias la='ls -A' |
| 170 | +alias ..='cd ..' |
| 171 | +</code></pre> |
| 172 | +<h3 id="43-functions">4.3 Functions</h3> |
| 173 | +<p>For more complex operations, use functions:</p> |
| 174 | +<pre><code class="language-bash">myfunc() { |
| 175 | + echo "Doing complex stuff!" |
| 176 | +} |
| 177 | +</code></pre> |
| 178 | +<h2 id="5-command-history">5. Command History</h2> |
| 179 | +<p>Command history allows you to recall and reuse previously executed commands.</p> |
| 180 | +<h3 id="51-navigating-history">5.1 Navigating History</h3> |
| 181 | +<ul> |
| 182 | +<li>Use Up and Down arrow keys to navigate through history.</li> |
| 183 | +<li><code>Ctrl+r</code>: Search backward through history.</li> |
| 184 | +</ul> |
| 185 | +<h3 id="52-repeating-commands">5.2 Repeating Commands</h3> |
| 186 | +<pre><code class="language-bash">!! # Repeat the last command |
| 187 | +!n # Repeat the nth command in history |
| 188 | +!-n # Repeat the nth last command |
| 189 | +</code></pre> |
| 190 | +<h3 id="53-modifying-history">5.3 Modifying History</h3> |
| 191 | +<pre><code class="language-bash">history -c # Clear current session's history |
| 192 | +history -d n # Delete the nth command from history |
| 193 | +history -a # Manually save session's history |
| 194 | +</code></pre> |
| 195 | +<h2 id="6-job-control">6. Job Control</h2> |
| 196 | +<p>Job control allows management of multiple processes within a single terminal session.</p> |
| 197 | +<h3 id="61-background-and-foreground-jobs">6.1 Background and Foreground Jobs</h3> |
| 198 | +<pre><code class="language-bash">command & # Start a job in the background |
| 199 | +Ctrl+Z # Pause the current foreground job |
| 200 | +fg %n # Bring job n to the foreground |
| 201 | +bg %n # Continue job n in the background |
| 202 | +</code></pre> |
| 203 | +<h3 id="62-listing-and-managing-jobs">6.2 Listing and Managing Jobs</h3> |
| 204 | +<pre><code class="language-bash">jobs # List all jobs |
| 205 | +kill %n # Terminate job n |
| 206 | +</code></pre> |
| 207 | +<h3 id="63-signals">6.3 Signals</h3> |
| 208 | +<pre><code class="language-bash">kill -l # List all available signals |
| 209 | +kill -SIGSTOP %n # Pause job n |
| 210 | +kill -SIGCONT %n # Resume job n |
| 211 | +kill -SIGKILL %n # Forcefully terminate job n |
| 212 | +</code></pre> |
| 213 | +<h2 id="7-scheduling-tasks">7. Scheduling Tasks</h2> |
| 214 | +<p>The <code>cron</code> utility allows scheduling of recurring tasks.</p> |
| 215 | +<h3 id="71-editing-the-crontab">7.1 Editing the Crontab</h3> |
| 216 | +<pre><code class="language-bash">crontab -e |
| 217 | +</code></pre> |
| 218 | +<h3 id="72-crontab-syntax">7.2 Crontab Syntax</h3> |
| 219 | +<pre><code>* * * * * command_to_execute |
| 220 | +│ │ │ │ │ |
| 221 | +│ │ │ │ └─── day of week (0 - 7) (Sunday = 0 or 7) |
| 222 | +│ │ │ └────── month (1 - 12) |
| 223 | +│ │ └─────────── day of month (1 - 31) |
| 224 | +│ └──────────────── hour (0 - 23) |
| 225 | +└───────────────────── minute (0 - 59) |
| 226 | +</code></pre> |
| 227 | +<h3 id="73-example-cron-job">7.3 Example Cron Job</h3> |
| 228 | +<pre><code>0 2 * * * /path/to/backup_script.sh |
| 229 | +</code></pre> |
| 230 | +<p>This runs the backup script every day at 2:00 AM.</p> |
| 231 | +<h2 id="8-best-practices">8. Best Practices</h2> |
| 232 | +<ol> |
| 233 | +<li>Use meaningful names for variables, aliases, and functions.</li> |
| 234 | +<li>Comment your scripts and complex commands for better readability.</li> |
| 235 | +<li>Be cautious when modifying system-wide environmental variables.</li> |
| 236 | +<li>Regularly review and clean up your command history and cron jobs.</li> |
| 237 | +<li>Use job control judiciously to manage system resources effectively.</li> |
| 238 | +<li>Test scripts and scheduled tasks thoroughly before implementation.</li> |
| 239 | +<li>Keep your shell configuration files (e.g., .bashrc, .zshrc) organized and well-commented.</li> |
| 240 | +<li>Use error handling in your scripts to make them more robust.</li> |
| 241 | +<li>When working with environmental variables, consider using lowercase for local variables and uppercase for exported variables to maintain clarity.</li> |
| 242 | +<li>Utilize shell script debugging tools like <code>set -x</code> for troubleshooting.</li> |
| 243 | +</ol> |
| 244 | +<h2 id="9-summary">9. Summary</h2> |
| 245 | +<p>Mastering these advanced command line features significantly enhances your ability to work efficiently in a Unix-like environment. From automating tasks with shell scripts to managing complex job workflows, these tools provide powerful capabilities for system administration, software development, and general productivity. </p> |
| 246 | +<p>Environmental variables offer a flexible way to configure your system and applications, while shell scripting allows you to automate complex tasks and create powerful utilities. Aliases and shortcuts streamline your workflow, and command history manipulation helps you work more efficiently. Job control gives you fine-grained management of processes, and task scheduling with cron allows for automation of recurring tasks.</p> |
| 247 | +<p>Regular practice and exploration of these features will continue to improve your command line proficiency. Remember to always consider security implications when working with sensitive data in scripts or environmental variables, and to document your work for future reference and collaboration.</p> |
| 248 | + |
| 249 | + </div> |
| 250 | + </div><footer> |
| 251 | + |
| 252 | + <hr/> |
| 253 | + |
| 254 | + <div role="contentinfo"> |
| 255 | + <!-- Copyright etc --> |
| 256 | + </div> |
| 257 | + |
| 258 | + Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. |
| 259 | +</footer> |
| 260 | + |
| 261 | + </div> |
| 262 | + </div> |
| 263 | + |
| 264 | + </section> |
| 265 | + |
| 266 | + </div> |
| 267 | + |
| 268 | + <div class="rst-versions" role="note" aria-label="Versions"> |
| 269 | + <span class="rst-current-version" data-toggle="rst-current-version"> |
| 270 | + |
| 271 | + |
| 272 | + |
| 273 | + </span> |
| 274 | +</div> |
| 275 | + <script src="../js/jquery-3.6.0.min.js"></script> |
| 276 | + <script>var base_url = "..";</script> |
| 277 | + <script src="../js/theme_extra.js"></script> |
| 278 | + <script src="../js/theme.js"></script> |
| 279 | + <script src="../search/main.js"></script> |
| 280 | + <script> |
| 281 | + jQuery(function () { |
| 282 | + SphinxRtdTheme.Navigation.enable(true); |
| 283 | + }); |
| 284 | + </script> |
| 285 | + |
| 286 | +</body> |
| 287 | +</html> |
0 commit comments