SlideShare ist ein Scribd-Unternehmen logo
1 von 10
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

Weitere ähnliche Inhalte

Was ist angesagt?

Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
Sudharsan S
 
Unix And C
Unix And CUnix And C
Unix And C
Dr.Ravi
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell Script
Dr.Ravi
 
Unix Basics
Unix BasicsUnix Basics
Unix Basics
Dr.Ravi
 
Shell programming 1.ppt
Shell programming  1.pptShell programming  1.ppt
Shell programming 1.ppt
Kalkey
 

Was ist angesagt? (20)

Rc - The Plan 9 Shell
Rc - The Plan 9 ShellRc - The Plan 9 Shell
Rc - The Plan 9 Shell
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basics
 
Unix lab manual
Unix lab manualUnix lab manual
Unix lab manual
 
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
 
Neoito — *NIX kungfu for web devs
Neoito — *NIX kungfu for web devsNeoito — *NIX kungfu for web devs
Neoito — *NIX kungfu for web devs
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Unix And C
Unix And CUnix And C
Unix And C
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Scripting ppt
Scripting pptScripting ppt
Scripting ppt
 
Unix shell scripting
Unix shell scriptingUnix shell scripting
Unix shell scripting
 
Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本
 
TDC2016SP - Become a jedi with PHP streams
TDC2016SP - Become a jedi with PHP streamsTDC2016SP - Become a jedi with PHP streams
TDC2016SP - Become a jedi with PHP streams
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell Script
 
Popstat1 sh
Popstat1 shPopstat1 sh
Popstat1 sh
 
Chap06
Chap06Chap06
Chap06
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell Scripting
 
Unix Basics
Unix BasicsUnix Basics
Unix Basics
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
De 0 a 100 con Bash Shell Scripting y AWK
De 0 a 100 con Bash Shell Scripting y AWKDe 0 a 100 con Bash Shell Scripting y AWK
De 0 a 100 con Bash Shell Scripting y AWK
 
Shell programming 1.ppt
Shell programming  1.pptShell programming  1.ppt
Shell programming 1.ppt
 

Andere mochten auch (6)

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

Ähnlich wie Scripting 101

Bash shell
Bash shellBash shell
Bash shell
xylas121
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
erbipulkumar
 
Unit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell scriptUnit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell script
root_fibo
 
Best training-in-mumbai-shell scripting
Best training-in-mumbai-shell scriptingBest training-in-mumbai-shell scripting
Best training-in-mumbai-shell scripting
vibrantuser
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
Raghu nath
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
Raghu nath
 

Ähnlich wie Scripting 101 (20)

390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
 
What is a shell script
What is a shell scriptWhat is a shell script
What is a shell script
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboy
 
First steps in C-Shell
First steps in C-ShellFirst steps in C-Shell
First steps in C-Shell
 
Bash shell
Bash shellBash shell
Bash shell
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.ppt
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
 
Unit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell scriptUnit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell script
 
Raspberry pi Part 25
Raspberry pi Part 25Raspberry pi Part 25
Raspberry pi Part 25
 
UNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming ConstructsUNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming Constructs
 
Best training-in-mumbai-shell scripting
Best training-in-mumbai-shell scriptingBest training-in-mumbai-shell scripting
Best training-in-mumbai-shell scripting
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Bash
BashBash
Bash
 
Shell programming
Shell programmingShell programming
Shell programming
 
Scripting ppt
Scripting pptScripting ppt
Scripting ppt
 
Best training-in-mumbai-shell scripting
Best training-in-mumbai-shell scriptingBest training-in-mumbai-shell scripting
Best training-in-mumbai-shell scripting
 

Kürzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

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