yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Shell Scripting Lesson 2


3m read
·Nov 3, 2024

Processing might take a few minutes. Refresh later.

Hey guys,

These matkins on one today is, um, shell scripting lesson. You should have watched the introduction to shell scripting, which was shell scripting lesson 1, and it basically explained what shell scripting was to you.

So in this video, I'm just going to dive right in, and I'm show you how to shell script. I will be using iTerm as my terminal application. You can use Terminal, but I prefer to use iTerm. Um, I will be using Mac Vi EM to edit my shell script. You can use Nano in Terminal or whatever you would like.

Here, here I go. I'm going to be showing you how to shell script, and I'm not actually going to be teaching you everything about shell scripting, but a few things in this shell scripting lesson. So I'll just demonstrate this to you. I'm using my favorite text editor, Mac Vi EM, so I'm going to be showing you shell scripting.

So you can make a text file however you want. You probably can just see DDO desktop and type nano hi.sh, and then you'll create a file called hi.sh and use nano as your editor. I'm just going to use a separate editor because it has a GUI. For those of you who haven't watched my terminal lessons, it'll be easier for you.

So any shell script we're writing, any shell program that's going to run in the shell, since we're using your shell as a programming language, is #!/bin/sh. That's just to say run this with sh, which is a shell program. You can change this from sh to bash if you like bash more, but I personally like sh.

Like in Terminal, if you type sh, then you're in a separate shell. And yeah, but if you like bash, you can change that to bash. I'm going to make it sa. You can also make it zsh, but that's all just kind of pointless. Oh, I'm just going to keep on editing this file.

So right here in the next line in shell scripting is all terminal commands. So if you want to make the shell script, first type the command ls /. You type that as the first line. And now, when this runs first over on ls /, now say you want to unzip a file. You go to the next line, type unzip space the path of that file, and you can do ~ for their home directory.

And then you can go on to the next line. Say before you want to do that unzip, you want to check if this file exists because otherwise it wouldn't be cool. This is where if statements come in. So we already know that we can run a command in Terminal like unzip, and we know that also in shell scripts we can run commands on individual lines.

But there are a few commands you didn't know about, like if or elif or fi. So I'll show them to you. So say we want to check if ~/desktop exists. What we want to do is type if space left bracket space dash f space and then the path of the file like ~/desktop/hi.zip, and then a space and then a closed bracket. Then you want to say then. So we do semicolon space then, and then we have the code you want under that.

Now at some point, we have to say, "Okay, this is the end of the if." So in order to do that, you say fi. So right now, this one line of code unzip ~/desktop/hi.zip will only happen if dash f ~/desktop/hi.zip is true. That means if ~/desktop/hi.zip is a file and it exists, then do these lines of codes between the if and the fi.

And then, yeah, so now if I add something under this, it says rm ~/desktop/hi.zip. This would run no matter what because it's outside of this fi line right there. That's important. So if we just grab this and put it right here, right in before the fi, then right here is inside of this if.

So, um, that's cool. So now we've successfully set up this thing so hi.zip exists on your desktop. Yeah, but that's kind of useless. So I'm going to be showing you just how to make a defense system that checks if a file has been deleted.

So first of all, let's say I'm going to be teaching you a few terminal commands with this as well. The first line of your code is while space bracket space true space close bracket, space too, then at the end of the file is done.

So everything... This is a while loop, so everything inside of these two... three line...

More Articles

View All
Analyzing motion problems: position | AP Calculus AB | Khan Academy
Divya received the following problem: A particle moves in a straight line with velocity ( v(t) ) is equal to the square root of ( 3t - 1 ) meters per second, where ( t ) is time in seconds. At ( t = 2 ), the particle’s distance from the starting point was…
Beautiful Animation Shows What It's Like to Be Homesick in a New Country | Short Film Showcase
Every spring, my mom used to plant boxes of violets and propagate the geranium she’s been growing for years in a small garden on a balcony in Tehran. I remember her telling me, “When you move a plant from one place to another, you need to give it some tim…
How to buy a private jet in 6 hours
I said, “Hey, you guys have a Gulf Stream 550 or 650 for sale? I’m interested in a jet. I got to take delivery by the end of the year.” And they’re like, that was a phone call he made to me. “Actually, we don’t keep jets around; we can’t deliver a plane t…
HARRY POTTER Full Movie 2024: Ambience | Superhero FXL Action Movies 2024 in English (Game Movie)
Merlin’s beard. How did you? Wait! We did… Hang on! Charge! The key. Give me your hand! Are you alright? You’re hurt. Perhaps a bit. Take this. It’s Wiganwell Potion. That stuff will heal you in a second. What happened? Poor George. I can’t believe he… Wh…
Confessions of a Tomb Robber | Lost Tombs of the Pyramids
Dr. Colleen Darnell hunts for clues to solve the mystery of why dozens of pharaohs were removed from their original tombs and reburied in an unmarked grave. [Music] Could this ancient papyrus hold the answer? One of the more remarkable documents to surv…
Why was George Washington the first president? | US History | Khan Academy
So in the early debates about the Constitution, there were folks that wanted a strong central leadership and other folks who didn’t because they felt it felt a lot like George III. How did the existence of Washington as a person affect the debate? It’s a…