SlideShare une entreprise Scribd logo
1  sur  31
Télécharger pour lire hors ligne
Bash4Beginners


                                  #!/bin/bash


                               echo “por: Lucas Souza Fernandes”




I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Just one day...
  ●   ~/.bash_history
  ●   man - How to get help
  ●   ~/.bashrc
  ●   Hello world...
  ●   Shell script
  ●   Debug mode on



I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Just one day...
  ●   Commands
  ●   test
  ●   Hands on
  ●   Prática




I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
~/.bash_history
  ●   Bourne Again Shell
      – BASH
  ●   Korn shell
  ●   C shell
  ●   IEEE Posix
                                               USUARIO → SHELL → KERNEL → HD




I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
man - How to get help... :S
  ●   Internet                                       ●   whereis
  ●   e-books                                        ●   which
  ●   apt-get install abs-                           ●   apropos
      guide                                                 –   apropos banner
  ●   man




I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
~/.bashrc
  ●   /etc/profile
  ●   ~/.bash_profile, ~/.bash_login or ~/.profile
  ●   ~/.bash_logout
  ●   cp /etc/skel/.bashrc ~/.bashrc
  ●   echo $?
  ●   echo $-
  ●   vi ~/.bashrc

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Hello world...

#!/bin/bash
echo Hello World
echo Bkp home
tar -cZf /tmp/my-backup.tgz /home/me




I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Hello world...

#!/bin/bash
echo Hello World
echo Bkp home
tar -cZf /tmp/my-backup.tgz /home/me




I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Hello world...


                        Seu bkp foi criado ???




I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Hello World...
#!/bin/bash
echo Hello World
echo Bkp home
tar -cZf /tmp/my-backup.tgz /home/me
If [ $? = 0 ] ; then
    echo Successfully completed
else
    echo Error...
fi


I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Shell script...
Um arquivo no qual temos uma lista de
comandos a serem executados, que podem
ser chamados a qualquer momento.

#!/bin/bash
date
df
w



I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Shell script...
  ●   Escolha um nome para seu script (filho);
  ●   Criar o arquivo e listar os comandos;
  ●   Evocando o shell na primeira linha:
              –   #!/bin/bash
  ●   Tornar o script um executável:
              –   chmod u+rx meuscript.sh
  ●   Executar seu script.


I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Debug mode on...
  ●   “Comando não encontrado”
              –   echo $PATH
  ●   “Permissão negada”
              –   chmod u+rx seuscript
  ●   “Erro de sintaxe”
              –   set -xv




I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Hello user...
#!/bin/bash
echo Hello User
echo Bkp your home? [yn]
read answer
test “$answer” = 'n' && exit
tar -cZf /tmp/my-backup.tgz /home/cefet
if [ $? = 0 ] ; then
    echo Successfully completed
else
    echo Error...
fi
I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Commands...
  ●   cat                                            ●   rev
  ●   cut                                            ●   sed
  ●   date                                           ●   seq
  ●   find                                           ●   sort
  ●   grep                                           ●   tail
  ●   head                                           ●   tr
  ●   printf                                         ●   uniq

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Commands...
  ●   wc
  ●   man comando
  ●   comando --help




I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
test...
      test variables                                 ●   ne → NotEqual
  ●   -lt → LessThan                                 ●   = → String
  ●   -gt → GreaterThan                              ●   != → Not equal
  ●   -le → LessEqual                                ●   -n → not Null
  ●   -ge →                                          ●   -z → is null
      GreaterEqual
  ●   -eq → EQual


I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
test...
      test files                                     ●   -ot → OlderThan
  ●   -d → directory ?                               ●   -ef → EqualFile
  ●   -f → file ?                                    ●   -a → AND
  ●   -r → read ?                                    ●   -o → OR
  ●   -s → file size > 0
  ●   -w → write ?
  ●   -nt → NeweThan

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Hands on...

    Let's rock... um script no qual o usuário
informe o nome do arquivo e o script irá testar
  se este arquivo existe. Se sim diz se é um
              arquivo ou diretório.




I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Hands on...

 ./argumento.sh arg1 arg2 arg3 arg4

 #!/bin/bash
 #argumentos – algumas variaveis especiais
 echo “o nome deste script é: $0”
 echo “recebidos $# argumentos: $*”
 echo “1st argumento: $1”
 echo “2sd argumento: $2”


I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Hands on...



                                         $((...))

                           echo $((2*3-2/2+3))




I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Hands on...


 if test “$var” -gt 10
     then
        echo maior que 10
 else
        echo menor que 10
 fi



I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Hands on...


 If [ “$var” -gt 10 ]
     then
        echo maior que 10
 else
        echo menor que 10
 fi



I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Hands on...


 while test -f /tmp/lock
 do
    echo “script travado...”
    sleep 1
 done




I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Hands on...
 while :
 do
    if test -f /tmp/lock
    then
        echo “ainda em lock...”
        sleep 1
    else
        break
    fi
 done

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Prática...
- Receba dois números como parametro e
mostre a relação entre eles.
./relacao.sh 3 5
3 é menor que 5

- Recebe um número como parametro e o
diminui até chegar a zero mostrando cada
passo na tela
./zerador.sh 5
543210

I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Prática...
- Recebe 2 palavras como parametro e
verifica se a primeira esta na segunda. (grep)
./substring.sh ana banana
ana esta contida em banana

- Exibe todos os paramentros recebidos
“juntos” (tr)
./juntatudo.sh o l a m u n d o c r u e l
olamundocruel


I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Prática...

 - Do arquivo /etc/passwd, mostra usuário e o
 nome completo, campos 1 e 5 separados por
 um TAB. (cut)
 ./users.sh
 ftpftp user
 nobodynobody
 lucas lucas souza fernandes


I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Prática...

 - Do arquivo /etc/passwd, mostra todos os
 shells (ultimo campo). (uniq)
 ./shell.sh
 /bin/bash
 /bin/false




I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Prática...
 Mostra na tela todos os parametros recebidos
 (shift)
 ./parametros a b c d e
 Parametro 1 : a
 Parametro 2 : b
 Parametro 3 : c
 Parametro 4 : d
 Parametro 5 : e


I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
Obrigado...




                        lucascoala@gmail.com




I CompILe MG 2009 - 9 de Setembro - Bash4Beginners

Contenu connexe

En vedette

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 

En vedette (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

CompileMG - Bash4beginners

  • 1. Bash4Beginners #!/bin/bash echo “por: Lucas Souza Fernandes” I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 2. Just one day... ● ~/.bash_history ● man - How to get help ● ~/.bashrc ● Hello world... ● Shell script ● Debug mode on I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 3. Just one day... ● Commands ● test ● Hands on ● Prática I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 4. ~/.bash_history ● Bourne Again Shell – BASH ● Korn shell ● C shell ● IEEE Posix USUARIO → SHELL → KERNEL → HD I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 5. man - How to get help... :S ● Internet ● whereis ● e-books ● which ● apt-get install abs- ● apropos guide – apropos banner ● man I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 6. ~/.bashrc ● /etc/profile ● ~/.bash_profile, ~/.bash_login or ~/.profile ● ~/.bash_logout ● cp /etc/skel/.bashrc ~/.bashrc ● echo $? ● echo $- ● vi ~/.bashrc I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 7. Hello world... #!/bin/bash echo Hello World echo Bkp home tar -cZf /tmp/my-backup.tgz /home/me I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 8. Hello world... #!/bin/bash echo Hello World echo Bkp home tar -cZf /tmp/my-backup.tgz /home/me I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 9. Hello world... Seu bkp foi criado ??? I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 10. Hello World... #!/bin/bash echo Hello World echo Bkp home tar -cZf /tmp/my-backup.tgz /home/me If [ $? = 0 ] ; then echo Successfully completed else echo Error... fi I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 11. Shell script... Um arquivo no qual temos uma lista de comandos a serem executados, que podem ser chamados a qualquer momento. #!/bin/bash date df w I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 12. Shell script... ● Escolha um nome para seu script (filho); ● Criar o arquivo e listar os comandos; ● Evocando o shell na primeira linha: – #!/bin/bash ● Tornar o script um executável: – chmod u+rx meuscript.sh ● Executar seu script. I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 13. Debug mode on... ● “Comando não encontrado” – echo $PATH ● “Permissão negada” – chmod u+rx seuscript ● “Erro de sintaxe” – set -xv I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 14. Hello user... #!/bin/bash echo Hello User echo Bkp your home? [yn] read answer test “$answer” = 'n' && exit tar -cZf /tmp/my-backup.tgz /home/cefet if [ $? = 0 ] ; then echo Successfully completed else echo Error... fi I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 15. Commands... ● cat ● rev ● cut ● sed ● date ● seq ● find ● sort ● grep ● tail ● head ● tr ● printf ● uniq I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 16. Commands... ● wc ● man comando ● comando --help I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 17. test... test variables ● ne → NotEqual ● -lt → LessThan ● = → String ● -gt → GreaterThan ● != → Not equal ● -le → LessEqual ● -n → not Null ● -ge → ● -z → is null GreaterEqual ● -eq → EQual I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 18. test... test files ● -ot → OlderThan ● -d → directory ? ● -ef → EqualFile ● -f → file ? ● -a → AND ● -r → read ? ● -o → OR ● -s → file size > 0 ● -w → write ? ● -nt → NeweThan I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 19. Hands on... Let's rock... um script no qual o usuário informe o nome do arquivo e o script irá testar se este arquivo existe. Se sim diz se é um arquivo ou diretório. I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 20. Hands on... ./argumento.sh arg1 arg2 arg3 arg4 #!/bin/bash #argumentos – algumas variaveis especiais echo “o nome deste script é: $0” echo “recebidos $# argumentos: $*” echo “1st argumento: $1” echo “2sd argumento: $2” I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 21. Hands on... $((...)) echo $((2*3-2/2+3)) I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 22. Hands on... if test “$var” -gt 10 then echo maior que 10 else echo menor que 10 fi I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 23. Hands on... If [ “$var” -gt 10 ] then echo maior que 10 else echo menor que 10 fi I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 24. Hands on... while test -f /tmp/lock do echo “script travado...” sleep 1 done I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 25. Hands on... while : do if test -f /tmp/lock then echo “ainda em lock...” sleep 1 else break fi done I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 26. Prática... - Receba dois números como parametro e mostre a relação entre eles. ./relacao.sh 3 5 3 é menor que 5 - Recebe um número como parametro e o diminui até chegar a zero mostrando cada passo na tela ./zerador.sh 5 543210 I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 27. Prática... - Recebe 2 palavras como parametro e verifica se a primeira esta na segunda. (grep) ./substring.sh ana banana ana esta contida em banana - Exibe todos os paramentros recebidos “juntos” (tr) ./juntatudo.sh o l a m u n d o c r u e l olamundocruel I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 28. Prática... - Do arquivo /etc/passwd, mostra usuário e o nome completo, campos 1 e 5 separados por um TAB. (cut) ./users.sh ftpftp user nobodynobody lucas lucas souza fernandes I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 29. Prática... - Do arquivo /etc/passwd, mostra todos os shells (ultimo campo). (uniq) ./shell.sh /bin/bash /bin/false I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 30. Prática... Mostra na tela todos os parametros recebidos (shift) ./parametros a b c d e Parametro 1 : a Parametro 2 : b Parametro 3 : c Parametro 4 : d Parametro 5 : e I CompILe MG 2009 - 9 de Setembro - Bash4Beginners
  • 31. Obrigado... lucascoala@gmail.com I CompILe MG 2009 - 9 de Setembro - Bash4Beginners