Notice
Recent Posts
Recent Comments
Link
«   2026/04   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
Archives
Today
Total
관리 메뉴

제니 블로그

Basic Scripting 본문

Building Blocks

Basic Scripting

jennystar 2023. 2. 15. 17:23

Scripting automates repetitive tasks and saves time. 

While learning programming languages, it should be necessary to know the basics of scripting. 


These scripts can be ran within a bash shell interpreter terminal.

Any command that can be run in terminal can be run in a bash script.

 

Beginning of the script file should start with:

#!/bin/bash/

This tells the computer which type of interpreter to use for the script.

 

The script files need to have the "execute" permission to allow them to be run.

To add the permission to a file with filename, run this command :

chmod +x filename.sh

Terminal runs a file every time it is opened to load its configurations.

On Linux style shells, this is ~/.bashrc

To ensure that scripts in ~/bin/ are avaialble, add this directory to the PATH with the configuration file

PATH=~/bin:$PATH
# Now any scripts in the ~/bin directory can be run from anywhere by typing the filename

 


Variables

Variables are declared by setting the variable name equal to another value

#no space between the variable name, equal sign, or string
gretting="hello"

To access the value of a variable, use variable name prepended with a dollar ($) sign

#to run the script on command line
./script.sh

Conditionals 

Conditionals can control which set of commands within the script run.

  1. Use if to start the conditional followed by the condition in square brackets [ ]
  2. have a space between the bracket and the conditional statement
  3. then begins the code that will run if condition is met
  4. else begins the code that will run if the condition is not met

> example code is shown below : 

# if $index is less than 5, print index, if it is greater or is 5, print 5
if [ $index -lt 5 ]
then 
	echo $index
else
	echo 5
fi

Bash script use a specific list of operators for comparison:

  • -eq : equal
  • -ne : not equal
  • -le : less than or equal
  • -lt : less than
  • -ge : greater than or equal
  • -gt : greater than
  • -z : is null

It is best to put variable into quotes `(")` to prevent errors if the variable is null or contain spaces

Comparing Strings : 

  • == : equal
  • != : not equal

Loops

3 different way to loop within bash script : for , while , until

for loop is used to iterate through a list and execute an action at each step 

# word is defined at the top of the for loop , no need to add `$` prepend
# need to prepend `$` when accessing value of the variables 
for word in $paragraph

#when accessing do block , accessing the variable = $
do 
	echo $word
done

Conditions are established the same way as they are within an `if` block between square brackets.


While

loops keep looping while the provided condition is true

while [ $index -lt 5 ]
do 
	echo $index
	index=$((index +1))
done

Until

loops loop until the condition is true

until [ $index -eq 5 ]
do 
	echo $index
	index=$((index + 1))
done

Nesting `while` with `if `

#!/bin/bash
first_greeting="Nice to meet you!"
later_greeting="How are you?"
greeting_occasion=0

while [ $greeting_occasion -lt 3 ] 
do 
  if [ $greeting_occasion -lt 1 ]
  then
    echo $first_greeting
  else
    echo $later_greeting
  fi
  greeting_occasion=$((greeting_occasion +1))
done

Inputs

To make bash scripts more useful, need to be able to access data external to the bash script file itself.

  1. prompt the user for input , use the read syntax. To ask the user for input and save it to the number variable , use the following code below
  2. Have the user input arguments when they run the script. arguments are entered after the script name and are separated by spaces
echo "Guess a number"
read number
echo "You guessed $number"

saycolors red green blue

Within the scripts these are accessed using $1 , $2 , $3

$1 = first argument and so on .... these are 1 indexed

If the script needs to accept an indefinite number of input arguments, can iterate over them using the `"$@"` syntax

for color in "$@"
do 
	echo $color
done

Inputs can also access external files to the script, can assign a set of files to a variable name using standard bash pattern matching using regular expressions 

files=/some/directory/*

#iterate through each file and do something
for files in $files
do 
	echo $file
done

Here we visited the very basics of shell scripting. I hope that I get to explore more of the basics as I go on!

'Building Blocks' 카테고리의 다른 글

Linux - Simple Commands and Navigation Part 1  (0) 2023.02.17
Basics of Github - Concepts  (0) 2023.02.16
Directories in Linux  (0) 2023.02.16
What is Git?  (0) 2023.02.16
Search Algorithms (Linear, Binary)  (0) 2023.02.15