<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>bash &#8211; abdulrahmanuk.com</title>
	<atom:link href="https://www.abdulrahmanuk.com/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.abdulrahmanuk.com</link>
	<description>All about Cloud and DevOps</description>
	<lastBuildDate>Wed, 31 May 2023 13:38:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.abdulrahmanuk.com/wp-content/uploads/2023/02/icons8-devops-60.png</url>
	<title>bash &#8211; abdulrahmanuk.com</title>
	<link>https://www.abdulrahmanuk.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Day-5 Task &#8211; Advanced Linux Shell Scripting for DevOps Engineers with User Management</title>
		<link>https://www.abdulrahmanuk.com/day-5-task-advanced-linux-shell-scripting-for-devops-engineers-with-user-management/</link>
					<comments>https://www.abdulrahmanuk.com/day-5-task-advanced-linux-shell-scripting-for-devops-engineers-with-user-management/#respond</comments>
		
		<dc:creator><![CDATA[Abdul Rahman UK]]></dc:creator>
		<pubDate>Wed, 31 May 2023 13:38:21 +0000</pubDate>
				<category><![CDATA[90DaysOfDevOps]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[devops]]></category>
		<guid isPermaLink="false">https://www.abdulrahmanuk.com/?p=370</guid>

					<description><![CDATA[<p>Challenge: If you noticed that there are total 90 sub directories in the directory &#8216;2023&#8217; of this repository. What did you think, how did I create 90 directories. Manually one by one or using a script, or a command ? All 90 directories within seconds using a simple command. mkdir day{1..90} Tasks So Write a [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.abdulrahmanuk.com/day-5-task-advanced-linux-shell-scripting-for-devops-engineers-with-user-management/">Day-5 Task &#8211; Advanced Linux Shell Scripting for DevOps Engineers with User Management</a> appeared first on <a rel="nofollow" href="https://www.abdulrahmanuk.com">abdulrahmanuk.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Challenge:</p>



<p>If you noticed that there are total 90 sub directories in the directory &#8216;2023&#8217; of this repository. What did you think, how did I create 90 directories. Manually one by one or using a script, or a command ?</p>



<p>All 90 directories within seconds using a simple command.</p>



<p><code>mkdir day{1..90}</code></p>



<h3 class="wp-block-heading"><a href="https://github.com/abdulrahman911/90DaysOfDevOps/blob/master/2023/day05/tasks.md#tasks" rel="nofollow noopener" target="_blank"></a>Tasks</h3>



<ol class="wp-block-list">
<li>You have to do the same using Shell Script i.e using either Loops or command with start day and end day variables using arguments &#8211;</li>
</ol>



<p>So Write a bash script createDirectories.sh that when the script is executed with three given arguments (one is directory name and second is start number of directories and third is the end number of directories ) it creates specified number of directories with a dynamic directory name.</p>



<p>Example 1: When the script is executed as</p>



<p><code>./createDirectories.sh day 1 90</code></p>



<p>then it creates 90 directories as&nbsp;<code>day1 day2 day3 .... day90</code></p>



<p>Example 2: When the script is executed as</p>



<p><code>./createDirectories.sh Movie 20 50</code> then it creates 50 directories as <code>Movie20 Movie21 Movie23 ...Movie50</code></p>



<p>As we know that if we want to pass any argument at the time of bash script execution we used to denote it as like&nbsp;<mark>$1(first arg),$2(2nd arg)</mark>&nbsp;etc&#8230;So here we will do the same thing in a bash script like :</p>



<p>name of the file as&nbsp;<strong><mark><a href="http://createdirectories.sh/" target="_blank" rel="noreferrer noopener nofollow">createDirectories.sh</a></mark></strong></p>



<p><strong><mark>start_day as $2 and end_day as $3 and day as $1</mark></strong></p>



<p><strong><mark>Execute as ./<a href="http://createdirectories.sh/" target="_blank" rel="noreferrer noopener nofollow">createDirectories.sh</a> day 1 90</mark></strong></p>



<p></p>



<p><strong><mark>Output</mark></strong></p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="2642" height="554" src="https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-11.png" alt="image 11" class="wp-image-371" title="Day-5 Task - Advanced Linux Shell Scripting for DevOps Engineers with User Management 1" srcset="https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-11.png 2642w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-11-768x161.png 768w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-11-1536x322.png 1536w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-11-2048x429.png 2048w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-11-150x31.png 150w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-11-300x63.png 300w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-11-696x146.png 696w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-11-1068x224.png 1068w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-11-1920x403.png 1920w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-11-600x126.png 600w" sizes="(max-width: 2642px) 100vw, 2642px" /></figure>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#a6accd;--cbp-line-number-width:17.8046875px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1b1e28"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="#!/bin/bash

if [ $# != 3 ]
then
	echo &quot;Can't proceed \n Please give three arguments to continue&quot;
	exit 1

fi

echo &quot;Creating directory as per your requirement&quot;
start_day=$2
end_day=$3

for (( i=start_day; i&lt;=end_day; i++ ))
do
	mkdir $1$i
done	

echo &quot;Here are your directories:&quot;
ls
" style="color:#a6accd;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki poimandres" style="background-color: #1b1e28"><code><span class="line"><span style="color: #A6ACCD">#!/bin/bash</span></span>
<span class="line"></span>
<span class="line"><span style="color: #A6ACCD">if [ </span><span style="color: #D0679D">$#</span><span style="color: #A6ACCD"> </span><span style="color: #D0679D">!=</span><span style="color: #A6ACCD"> </span><span style="color: #5DE4C7">3</span><span style="color: #A6ACCD"> ]</span></span>
<span class="line"><span style="color: #A6ACCD">then</span></span>
<span class="line"><span style="color: #A6ACCD">	echo </span><span style="color: #A6ACCD">&quot;</span><span style="color: #5DE4C7">Can&#39;t proceed </span><span style="color: #5FB3A1">\n</span><span style="color: #5DE4C7"> Please give three arguments to continue</span><span style="color: #A6ACCD">&quot;</span></span>
<span class="line"><span style="color: #A6ACCD">	exit </span><span style="color: #5DE4C7">1</span></span>
<span class="line"></span>
<span class="line"><span style="color: #A6ACCD">fi</span></span>
<span class="line"></span>
<span class="line"><span style="color: #A6ACCD">echo </span><span style="color: #A6ACCD">&quot;</span><span style="color: #5DE4C7">Creating directory as per your requirement</span><span style="color: #A6ACCD">&quot;</span></span>
<span class="line"><span style="color: #A6ACCD">start_day=$</span><span style="color: #5DE4C7">2</span></span>
<span class="line"><span style="color: #A6ACCD">end_day=$</span><span style="color: #5DE4C7">3</span></span>
<span class="line"></span>
<span class="line"><span style="color: #A6ACCD">for (( i=start_day; i&lt;=end_day; i++ ))</span></span>
<span class="line"><span style="color: #A6ACCD">do</span></span>
<span class="line"><span style="color: #A6ACCD">	mkdir $</span><span style="color: #5DE4C7">1</span><span style="color: #A6ACCD">$i</span></span>
<span class="line"><span style="color: #A6ACCD">done	</span></span>
<span class="line"></span>
<span class="line"><span style="color: #A6ACCD">echo </span><span style="color: #A6ACCD">&quot;</span><span style="color: #5DE4C7">Here are your directories:</span><span style="color: #A6ACCD">&quot;</span></span>
<span class="line"><span style="color: #A6ACCD">ls</span></span>
<span class="line"></span></code></pre></div>



<p><strong>2. Create a Script to back up all your work done till now.</strong></p>



<p>Backups are an important part of DevOps Engineers&#8217; day to Day activities. Usually, when we create any backup of a file we used to create a zip file by using the tar command:&nbsp;<strong>tar -czvf file.tar.gz directory_name</strong></p>



<p>Here is a simple shell script to take a backup of recent work:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#a6accd;--cbp-line-number-width:17.8046875px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1b1e28"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="#!/bin/bash

src_directory=/var/www/my-blog

BACKUP_FILENAME=&quot;backup_$(date +%Y-%m-%d).tar.gz&quot;
TARGET_DIRECTORY=&quot;/home/ubuntu/backup/&quot;

#Create backup archive
tar -czvf $TARGET_DIRECTORY/$BACKUP_FILENAME $src_directory

if [ $? -eq 0 ]; then
	echo &quot;Backup successful!&quot;
else
	echo &quot;Backup failed.&quot;
fi

" style="color:#a6accd;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki poimandres" style="background-color: #1b1e28"><code><span class="line"><span style="color: #A6ACCD">#!/bin/bash</span></span>
<span class="line"></span>
<span class="line"><span style="color: #A6ACCD">src_directory=/var/www/my-blog</span></span>
<span class="line"></span>
<span class="line"><span style="color: #A6ACCD">BACKUP_FILENAME=</span><span style="color: #A6ACCD">&quot;</span><span style="color: #5DE4C7">backup_$(date +%Y-%m-%d).tar.gz</span><span style="color: #A6ACCD">&quot;</span></span>
<span class="line"><span style="color: #A6ACCD">TARGET_DIRECTORY=</span><span style="color: #A6ACCD">&quot;</span><span style="color: #5DE4C7">/home/ubuntu/backup/</span><span style="color: #A6ACCD">&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #A6ACCD">#Create backup archive</span></span>
<span class="line"><span style="color: #A6ACCD">tar -czvf $TARGET_DIRECTORY/$BACKUP_FILENAME $src_directory</span></span>
<span class="line"></span>
<span class="line"><span style="color: #A6ACCD">if [ </span><span style="color: #D0679D">$?</span><span style="color: #A6ACCD"> </span><span style="color: #D0679D">-eq</span><span style="color: #A6ACCD"> </span><span style="color: #5DE4C7">0</span><span style="color: #A6ACCD"> ]; then</span></span>
<span class="line"><span style="color: #A6ACCD">	echo </span><span style="color: #A6ACCD">&quot;</span><span style="color: #5DE4C7">Backup successful!</span><span style="color: #A6ACCD">&quot;</span></span>
<span class="line"><span style="color: #A6ACCD">else</span></span>
<span class="line"><span style="color: #A6ACCD">	echo </span><span style="color: #A6ACCD">&quot;</span><span style="color: #5DE4C7">Backup failed.</span><span style="color: #A6ACCD">&quot;</span></span>
<span class="line"><span style="color: #A6ACCD">fi</span></span>
<span class="line"></span>
<span class="line"></span></code></pre></div>



<p><strong>3. Read About Cron and Crontab, to automate the backup Script</strong></p>



<p>Cron is a system process that will automatically perform tasks as per the specific schedule. It is a set of commands that are used for running regular scheduling tasks. Crontab stands for “<mark>cron table</mark>”. It allows using a job scheduler, which is known as cron to execute tasks.</p>



<p>Crontab is also the name of the program, which is used to edit that schedule. It is driven by a crontab file, a config file that indicates shell commands to run periodically for the specific schedule.</p>



<p><strong><mark>Reasons for using Cron jobs:</mark></strong></p>



<ul class="wp-block-list">
<li>Helps OS to take a scheduled backup of log files or database.</li>



<li>Delete old log files</li>



<li>Archive and purge database tables</li>



<li>Send out any notification email such as Newsletters, Password expiration email</li>



<li>Regular clean-up of cached data</li>



<li>Crontab is an ideal option to automate Unix jobs.</li>



<li>It is used to automate system maintenance</li>
</ul>



<p><strong>Crontab syntax:</strong></p>



<p><strong><mark>MIN HOUR DOM MON DOW CMD</mark></strong></p>



<p><strong>Ranges :</strong></p>



<figure class="wp-block-image size-full"><img decoding="async" width="1262" height="516" src="https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-13.png" alt="image 13" class="wp-image-373" title="Day-5 Task - Advanced Linux Shell Scripting for DevOps Engineers with User Management 2" srcset="https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-13.png 1262w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-13-768x314.png 768w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-13-150x61.png 150w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-13-300x123.png 300w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-13-696x285.png 696w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-13-1068x437.png 1068w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-13-600x245.png 600w" sizes="(max-width: 1262px) 100vw, 1262px" /></figure>



<figure class="wp-block-image size-full"><img decoding="async" width="996" height="466" src="https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-14.png" alt="image 14" class="wp-image-374" title="Day-5 Task - Advanced Linux Shell Scripting for DevOps Engineers with User Management 3" srcset="https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-14.png 996w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-14-768x359.png 768w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-14-150x70.png 150w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-14-300x140.png 300w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-14-696x326.png 696w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-14-600x281.png 600w" sizes="(max-width: 996px) 100vw, 996px" /></figure>



<p><strong>4. Read about User Management</strong></p>



<p>User management includes everything from creating a user to deleting a user on your system.</p>



<h3 class="wp-block-heading" id="heading-root"><a href="https://tanaya.hashnode.dev/advanced-linux-shell-scripting-for-devops-engineers-with-user-management-day-5-task#heading-root" rel="nofollow noopener" target="_blank"></a><strong>root</strong></h3>



<p>The root user is the superuser and has all the powers for creating a user, deleting a user and can even login in with the other user&#8217;s account. The root user always has userid 0.</p>



<h3 class="wp-block-heading" id="heading-useradd"><a href="https://tanaya.hashnode.dev/advanced-linux-shell-scripting-for-devops-engineers-with-user-management-day-5-task#heading-useradd" rel="nofollow noopener" target="_blank"></a><strong>useradd</strong></h3>



<p>1. With useradd command, you can add a user.</p>



<p>Syntax:&nbsp;<strong><mark>sudo adduser username [you should have the super user power to add a user]</mark></strong></p>



<p><strong>Then</strong>&nbsp;<mark>Enter the password for the new account and confirm</mark></p>



<p>You can check the newly added user from the file&nbsp;<strong><mark>/etc/passwd</mark></strong></p>



<p>Command:&nbsp;<strong>cat /etc/passwd</strong></p>



<p>2.&nbsp;<strong>For disabling an account using Terminal, remove the password set on the account.</strong></p>



<p><strong><mark>sudo passwd -l &#8216;username&#8217;</mark></strong></p>



<p>3. To delete an account, use the command –</p>



<p><strong><mark>sudo userdel -r &#8216;username</mark></strong></p>



<h2 class="wp-block-heading" id="heading-5-create-2-users-and-just-display-their-usernames"><strong>5. Create 2 users and just display their Usernames</strong></h2>



<p>Added two users by using the useradd command :</p>



<p>$ sudo useradd Sam</p>



<p>$ sudo useradd Vinod</p>



<p>To display their name use <strong>cat /etc/passwd</strong> and redirected the output to the <strong>tail</strong> command to see only the last two lines from the file :</p>



<p>To display their name use <strong>cat /etc/passwd</strong> and redirected the output to the <strong>tail</strong> command to see only the last two lines from the file :</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1606" height="356" src="https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-15.png" alt="image 15" class="wp-image-375" title="Day-5 Task - Advanced Linux Shell Scripting for DevOps Engineers with User Management 4" srcset="https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-15.png 1606w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-15-768x170.png 768w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-15-1536x340.png 1536w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-15-150x33.png 150w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-15-300x67.png 300w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-15-696x154.png 696w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-15-1068x237.png 1068w, https://www.abdulrahmanuk.com/wp-content/uploads/2023/05/image-15-600x133.png 600w" sizes="auto, (max-width: 1606px) 100vw, 1606px" /></figure>



<h2 class="wp-block-heading" id="heading-5-create-2-users-and-just-display-their-usernames"><a href="https://tanaya.hashnode.dev/advanced-linux-shell-scripting-for-devops-engineers-with-user-management-day-5-task#heading-5-create-2-users-and-just-display-their-usernames" rel="nofollow noopener" target="_blank"></a></h2>
<p>The post <a rel="nofollow" href="https://www.abdulrahmanuk.com/day-5-task-advanced-linux-shell-scripting-for-devops-engineers-with-user-management/">Day-5 Task &#8211; Advanced Linux Shell Scripting for DevOps Engineers with User Management</a> appeared first on <a rel="nofollow" href="https://www.abdulrahmanuk.com">abdulrahmanuk.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.abdulrahmanuk.com/day-5-task-advanced-linux-shell-scripting-for-devops-engineers-with-user-management/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
