SlideShare a Scribd company logo
Scripting 101
My First Sony ^H^H^H hellscript

#!/bin/bash
echo “Hello World!”




$ sh script.sh
 Hello World
Output redirection
There are 3 file descriptors:               Examples:
   stdin, stdout and stderr.
                                            1.    ls -l > ls-l.txt
Basically you can:                          2.   grep da * 2> grep-errors.txt
    redirect stdout to a file              3.   grep da * 1>&2
    redirect stderr to a file              4.   grep * 2>&1
    redirect stdout to a stderr            5.   rm -f $(find / -name core) &>
                                                 /dev/null
    redirect stderr to a stdout
    redirect stderr and stdout to a file
    redirect stderr and stdout to
     stdout
    redirect stderr and stdout to stderr

1 'represents' stdout and 2 stderr.
Pipes

Pipes can redirect output from one command to
  another

Example:

$ cat /etc/passwd | grep „whoami‟
Variables
STR=“Hello World!”
OF=“/var/my-backup-$(date +%Y%m%d).tgz”



#!/bin/bash
STR=“Hello World!”
echo $STR



#!/bin/bash
OF=“/var/my-backup-$(date +%Y%m%d).tgz”
tar -cZf $OF /home/me/
Local variables

#!/bin/bash
HELLO=Hello
function hello {
    local HELLO=World
    echo $HELLO
}
echo $HELLO
hello
echo $HELLO
Conditions
if [expression];                    Examples:
then
    code if 'expression' is true.   #!/bin/bash
                                    if [ "foo" = "foo" ]; then
fi                                       echo “expression
                                         evaluated as true”
                                    fi


                                    #!/bin/bash
if … then                           T1="foo"
else                                T2="bar"
                                    if [ "$T1" = "$T2" ]; then
elseif                                   echo “expression
                                         evaluated as true”
                                    else
                                           echo “expression
                                           evaluated as false”
                                    fi
Loops

3 kinds of loops   Examples:

                   #!/bin/bash
 for loop         for i in `seq 1 10`; do
                     echo $i
 while loop       Done
 until loop
                   #!/bin/bash
                   COUNTER=0
                   while [ $COUNTER -lt 10 ]; do
                     echo “The counter is “ $COUNTER
                     let COUNTER=COUNTER+1
                   done
Assignment:

Write a linux shell script to automagically
create 1000 user accounts. Make sure their
default password is “Hello123”

(hint: you cannot set the pasword with the –p
option….)
Useful files and commands

nano, joe, vi (editors)
/etc/passwd
useradd
passwd
rm -rf

More Related Content

PDF
003 scripting
PDF
COSCUP2012: How to write a bash script like the python?
PPT
01 linux basics
PDF
TDC São Paulo 2016 - Become a jedi with php streams
PDF
Module 03 Programming on Linux
PPT
Shell Scripts
PDF
Unix 1st sem lab programs a - VTU Karnataka
PDF
03 tk2123 - pemrograman shell-2
003 scripting
COSCUP2012: How to write a bash script like the python?
01 linux basics
TDC São Paulo 2016 - Become a jedi with php streams
Module 03 Programming on Linux
Shell Scripts
Unix 1st sem lab programs a - VTU Karnataka
03 tk2123 - pemrograman shell-2

What's hot (20)

PDF
Rc - The Plan 9 Shell
PPTX
Unix shell scripting basics
RTF
Unix lab manual
PDF
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
PDF
Neoito — *NIX kungfu for web devs
PPT
Unix Shell Scripting Basics
PPT
Unix And C
PPT
Shell Scripting
PPTX
Scripting ppt
PPTX
Unix shell scripting
PDF
Shell实现的windows回收站功能的脚本
PDF
TDC2016SP - Become a jedi with PHP streams
PPT
Talk Unix Shell Script
TXT
Popstat1 sh
PPT
Chap06
PPT
Unix And Shell Scripting
PPT
Unix Basics
PDF
Shell scripting
PDF
De 0 a 100 con Bash Shell Scripting y AWK
PPTX
Shell programming 1.ppt
Rc - The Plan 9 Shell
Unix shell scripting basics
Unix lab manual
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Neoito — *NIX kungfu for web devs
Unix Shell Scripting Basics
Unix And C
Shell Scripting
Scripting ppt
Unix shell scripting
Shell实现的windows回收站功能的脚本
TDC2016SP - Become a jedi with PHP streams
Talk Unix Shell Script
Popstat1 sh
Chap06
Unix And Shell Scripting
Unix Basics
Shell scripting
De 0 a 100 con Bash Shell Scripting y AWK
Shell programming 1.ppt
Ad

Viewers also liked (6)

PPT
adele someone like you lyrics
PPT
Mesa: "Las vacunas y los trasplantes ¿son servicios básicos de salud o un luj...
PPTX
JOSE AVILA - Utilidad del Big Data en predicción de riesgos
PPTX
JULIO MAYOL - ¿Es necesario monitorizar todo?
PPT
Atlantica XXII se presenta en Cangas de Onis (2009) con Xuán Cándano y Fernan...
PDF
"La Salud un manantial de risas"
adele someone like you lyrics
Mesa: "Las vacunas y los trasplantes ¿son servicios básicos de salud o un luj...
JOSE AVILA - Utilidad del Big Data en predicción de riesgos
JULIO MAYOL - ¿Es necesario monitorizar todo?
Atlantica XXII se presenta en Cangas de Onis (2009) con Xuán Cándano y Fernan...
"La Salud un manantial de risas"
Ad

Similar to Scripting 101 (20)

PPT
390aLecture05_12sp.ppt
DOCX
What is a shell script
PDF
Unleash your inner console cowboy
PPTX
First steps in C-Shell
PPT
34-shell-programming asda asda asd asd.ppt
PPT
Bash shell
PPT
34-shell-programming.ppt
PPT
2-introduction_to_shell_scripting
PDF
Operating_System_Lab_ClassOperating_System_2.pdf
PDF
shell scripting crash course pdf for engineers
PDF
Unit 11 configuring the bash shell – shell script
PPTX
Raspberry pi Part 25
PPS
UNIX - Class3 - Programming Constructs
PPT
Best training-in-mumbai-shell scripting
PPTX
Bash Shell Scripting
PPTX
Linux Shell Scripting
PPTX
Bash Shell Scripting
PDF
Shell scripting
PDF
Shell scripting
390aLecture05_12sp.ppt
What is a shell script
Unleash your inner console cowboy
First steps in C-Shell
34-shell-programming asda asda asd asd.ppt
Bash shell
34-shell-programming.ppt
2-introduction_to_shell_scripting
Operating_System_Lab_ClassOperating_System_2.pdf
shell scripting crash course pdf for engineers
Unit 11 configuring the bash shell – shell script
Raspberry pi Part 25
UNIX - Class3 - Programming Constructs
Best training-in-mumbai-shell scripting
Bash Shell Scripting
Linux Shell Scripting
Bash Shell Scripting
Shell scripting
Shell scripting

Recently uploaded (20)

PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Electronic commerce courselecture one. Pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
MYSQL Presentation for SQL database connectivity
PDF
cuic standard and advanced reporting.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Big Data Technologies - Introduction.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Unlocking AI with Model Context Protocol (MCP)
Diabetes mellitus diagnosis method based random forest with bat algorithm
Electronic commerce courselecture one. Pdf
NewMind AI Weekly Chronicles - August'25 Week I
Programs and apps: productivity, graphics, security and other tools
Spectroscopy.pptx food analysis technology
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
The AUB Centre for AI in Media Proposal.docx
Reach Out and Touch Someone: Haptics and Empathic Computing
MYSQL Presentation for SQL database connectivity
cuic standard and advanced reporting.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
sap open course for s4hana steps from ECC to s4
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf

Scripting 101

  • 2. My First Sony ^H^H^H hellscript #!/bin/bash echo “Hello World!” $ sh script.sh  Hello World
  • 3. Output redirection There are 3 file descriptors: Examples: stdin, stdout and stderr. 1. ls -l > ls-l.txt Basically you can: 2. grep da * 2> grep-errors.txt  redirect stdout to a file 3. grep da * 1>&2  redirect stderr to a file 4. grep * 2>&1  redirect stdout to a stderr 5. rm -f $(find / -name core) &> /dev/null  redirect stderr to a stdout  redirect stderr and stdout to a file  redirect stderr and stdout to stdout  redirect stderr and stdout to stderr 1 'represents' stdout and 2 stderr.
  • 4. Pipes Pipes can redirect output from one command to another Example: $ cat /etc/passwd | grep „whoami‟
  • 5. Variables STR=“Hello World!” OF=“/var/my-backup-$(date +%Y%m%d).tgz” #!/bin/bash STR=“Hello World!” echo $STR #!/bin/bash OF=“/var/my-backup-$(date +%Y%m%d).tgz” tar -cZf $OF /home/me/
  • 6. Local variables #!/bin/bash HELLO=Hello function hello { local HELLO=World echo $HELLO } echo $HELLO hello echo $HELLO
  • 7. Conditions if [expression]; Examples: then code if 'expression' is true. #!/bin/bash if [ "foo" = "foo" ]; then fi echo “expression evaluated as true” fi #!/bin/bash if … then T1="foo" else T2="bar" if [ "$T1" = "$T2" ]; then elseif echo “expression evaluated as true” else echo “expression evaluated as false” fi
  • 8. Loops 3 kinds of loops Examples: #!/bin/bash  for loop for i in `seq 1 10`; do echo $i  while loop Done  until loop #!/bin/bash COUNTER=0 while [ $COUNTER -lt 10 ]; do echo “The counter is “ $COUNTER let COUNTER=COUNTER+1 done
  • 9. Assignment: Write a linux shell script to automagically create 1000 user accounts. Make sure their default password is “Hello123” (hint: you cannot set the pasword with the –p option….)
  • 10. Useful files and commands nano, joe, vi (editors) /etc/passwd useradd passwd rm -rf