Header AD

How To Execute Commands In A Shell Script

How To Execute Commands in shell script

How To Execute Commands In A Shell Script
How To Execute Commands In A Shell Script

How To Execute Commands In A Shell Script

Until now have been understanding shell scripting's basic concepts and syntax.We have not done anything productive yet, but we are going to have some fun in this article. We will be creating very short but useful shell scripts.
In this article, we will be automating Linux's tasks.All the commands that we run or execute from terminal we can actually place them in a bash file in some logical order and execute.

Place commands in a bash file and run

Yes as simple as that put the commands that you like the most in a file or follow me. Save and execute the following script.
#!/bin/bash
ls -l
mkdir linux java
free -m
who
On successful completion, all the commands will be executed.ls will list all the content of your current directory.mkdir will create 2 directories java and Linux, free -m will show free ram, disk, swap sapce in mega bytes and who will list currently logged in users.
In this way you can automate your Linux tasks.You can be more creative here .Use your imagination and make it useful.

Bash Script for cleaning, updating and upgrading Linux

This is definitely useful .We have to update Linux so often so we have to type some lengthy commands so let's wrap those into a single command. Save and execute the following command.
apt-get clean && apt-get update && apt-get upgrade -y && apt-get dist-upgrade -y
Above script will clean your unnecessary packages then it will download and update the latest packages and finally if new distribution is available it will upgrade that.

Copy files with shell script

#!/bin/bash

echo "Enter file  to be copied"
read old 
echo "Enter new file name "
read new
if cp $old $new 
 then 
echo "file successfully copied"
else 
echo "file does not exits"
fi
  • First line will ask user to enter the name that you want to make copy of.File must be on your present working directory.
  • Second input line will store the back up file name .
  • if statement will check if operation has successfully been performed or not, if it's performed then successful message will be printed on your output screen.
In the way guys you can use mv command for renaming and -r for copying and renaming directories.


So above are the How To Execute Commands in a shell script. Hope you like this article, keep on sharing with others too. Also, share your experience with us in a comment box below.

No comments