top of page

Get Help In Shell Script Assignments and Projects | What is Shell Script? | Realcode4you

What is Shell Scripting?

  • A shell script is a series of commands aggregated within a single structured file

  • A command shell executes this file line-by-line, one command at a time as if individually executed on the command line

  • A shell script may use control structures, variables and dynamic inputs to control how and when commands are executed

  • In this sense, shell scripting is a form of computer programming

  • Shell scripts allow users to automate repetitive, multi-step tasks and often complex tasks on a Linux installation

  • Shell script is also used to create custom tools designed to perform highly specific tasks for which no pre-made solution is available

  • Scripts are usually designed to be executed in a command-line interface, although they can be triggered in other ways, e.g. a scheduled chron job.


Shell Script Commands, Keywords and Utilities

  • The bash commands, keywords and utilities that follow are those you will use to complete your workshop activities and assignments

  • You may not need to use all of those listed, but you will probably end up using most of them

  • Most, but not all, are specifically addressed in the unit’s lecture materials

  • You will find that the definitions and usage of those that are not can be found at https://ss64.com/bash/






Essential bash Commands

mkdir

  • The mkdir command is used to create directories

  • The flags commonly associated with the mkdir command are shown in the table

EXAMPLE:

mkdir -m 755 -v mydirectory
  • This command creates the directory mydirectory with permissions set to read, write, and execute for the owner, and read and execute for others

  • Additionally, it will display a message indicating that the directory has been created


cat

  • The cat command (short for concatenate) is used to display the contents of one or multiple files on the standard output

  • While it doesn't have many flags of its own, it can be combined with other commands or features to achieve specific functionality.


Cat options example :



ls

  • The ls command is used to list the files and directories that reside within a specified directory

  • It provides a number of options that allow its output and behavior to be modified from the defaults, with the most common being:



cp

  • The cp command is used to copy files and directories

  • It provides several options (flags) to control its behavior, the most common being:



mv

  • The mv command in bash is used to move or rename files and directories

  • It provides several options (flags) to control its behavior, the most common being:


rm

  • The rm command is used to remove/delete files and directories

  • It provides several options (flags) to control its behavior, the most common being:



Writing a bash shell script

  • Most programming beginners start out by writing a simple program to output the words Hello World on the screen

  • In bash scripting, this can be accomplished with the script shown to right



Executing the script

  • In most operating systems, files are not allowed to be directly executed as scripts by default

  • Most scripts will need to have the correct permissions set to allow them to be executed

  • A script without the execute permissions can be manually executed by invoking the bash command but this is not ideal

$ bash hello1.sh 

  • Permissions can be set using the chmod command using chmod +x hello1.sh or chmod 7[n][n] hello1.sh and only needs to be done once

  • After the file is marked as executable, the script can be run by prepending it with ./ and hitting Enter, i.e. ./hello1.sh


Writing and Executing a bash Script

Using the VS Code development environment:



Working with default variables and arguments

  • Information in bash scripts can be stored in variables

  • Once information is stored in a variable, it can be called upon in the script by invoking the variable’s name


bottom of page