Friday, April 22, 2022

Noob Creating a Shell Script

Using the terminal can be a pain in the ass. So let's make a shell script, so we can avoid typing in the same commands over and over again. Connect to your terminal and log in, then type this...
cd ~
...or wherever you want, "~" is the users home directory...
mkdir shellScript
cd shellScript
...note: while you're typing in a file or folder name, type a few letters and press the TAB button, the rest of the name should appear automatically, unless there's another name that starts the same.
Create a script file: Both "vim" and "nano" are file editors in Linux, you probably have both. Either way they are easy to install, I'm using "nano".
so...
nano scriptFile.sh
... this will create a file and open the "nano" editor, behold you're editing a file, type this...
echo "gd"
...press control+X, you will be asked to save? press "y", then press enter to save as the current file name. You're back in the terminal...
bash scriptFile.sh
...the file runs and "gd" is echoed to the terminal, wow. So you don't like having to type "bash" before the filename to run it? No problem, just do this...
nano scriptFile.sh
...add this line at the top of the file...
#!/bin/bash
...press: control+X, y, enter - file updated! Now all you have to do to run it is...
./scriptFile.sh
...notice you need the "./" before the filename now?
Haha got you, the file doesn't have permission to do anything, lol...
chmod +x scriptFile.sh
...permission granted. Now just type...
./scriptFile.sh
...wow it worked! So easy.
Now just type...
rm scriptFile.sh
...great, now that piece of garbage file is gone!

0 comments:

Post a Comment

Delicious Digg Stumbleupon Favorites