Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Linux dalla Console - terza serata
Search
BgLUG
April 05, 2017
Programming
360
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Linux dalla Console - terza serata
Bash scripting
BgLUG
April 05, 2017
More Decks by BgLUG
See All by BgLUG
Introduzione a Python – Ecosistema Python e usi specifici
bglug
1
310
Introduzione a Python – Funzionalità di base e strutture dati
bglug
1
310
Linux dalla Console - quarta serata
bglug
0
230
Linux dalla Console - seconda serata
bglug
1
300
Linux dalla Console - prima serata
bglug
1
370
Other Decks in Programming
See All in Programming
FactoryBotをやめればRSpecは速くなるのか?
yuta_onishi_97
0
270
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
750
ChromeでローカルLLM
ryu1013
0
140
【加筆修正版】Laravel のアプリケーションをどこにデプロイするか #phpcon_ehime
akase244
0
150
Findy - エンジニア向け会社紹介/Findy Company Deck
findyinc
6
400k
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
420
気持ちぃ〜角丸 Squircle
ryu1013
0
310
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
160
AIで開発が速くなった先で、開発チームは何を目指すか
shihochan
0
880
Event is what happens
moro
2
770
Unity6 URPによる海底をテーマにしたVJシステムの解説 | Unity Shader 完全に理解した 勉強会
gam0022
0
370
SalesForceを内製化!? ~ HR事業を支える顧客管理基盤のインフラを大公開 ~
oku053
0
180
Featured
See All Featured
Google's AI Overviews - The New Search
badams
0
1.6k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
460
StorybookのUI Testing Handbookを読んだ
zakiyama
31
7k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.3k
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
A Modern Web Designer's Workflow
chriscoyier
699
190k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
450
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
320
It's Worth the Effort
3n
188
29k
Music & Morning Musume
bryan
48
7.4k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Transcript
Linux dalla console — 3 Bash scripting Emiliano Vavassori 5
aprile 2017 FabLab Bergamo Patronato San Vincenzo, Bergamo Bergamo Linux Users Group 1
Sommario Generalità sullo scripting Programmazione in bash Variabili Controllo di
flusso Cicli Subshell Input dall’utente Argomenti Lavorare con i file 2
Generalità sullo scripting
Generalità Uno script è un file di testo contenente istruzioni
che un interprete (ad esempio la shell) esegue nell’ordine in cui le stesse istruzioni sono state inserite. bash può interpretare comandi interni o esterni: comandi propri della shell (if, for, test, …) o comandi esterni (ls, mkdir, grep, awk, …). 3
Cenni sull’esecuzione batch $ bash -c '<comando 1>; <comando 2>;
\ <comando 3>' 4
Qualche trucco per eseguire più comandi… $ <comando 1> &&
<comando 2> || <comando 3> $ <comando molto lungo> & $ fg $ nohup <comando> >logfile 5
Anatomia di uno script Estratto: nomescript.sh #!/bin/bash # Commenti <comando
1> <comando 2> <comando 3> <comando 4> # Ulteriori comandi 6
Eseguire uno script $ bash nomescript.sh 7
Eseguire uno script — Eseguibile? $ chmod +x nomescript.sh $
./nomescript.sh 8
Qualche altro trucco sugli eseguibili… Attenzione a chi do accesso
in esecuzione! $ chmod +x nomefile.sh || chmod u+x nomefile.sh Percorso di sistema — $PATH $ cp nomescript.sh $HOME/bin/; cd /tmp $ nomescript.sh 9
Programmazione in bash
Definizione delle variabili $ miavariabile="ciccio pasticcio" Variabili d’ambiente: $ export
MIAVAR="valore" 10
Distruzione di una variabile $ unset MIAVAR 11
Uso delle variabili $ echo "$miavariabile" > ciccio.txt $ echo
"${miavariabile}" > ciccio.txt 12
Controllo di flusso — if $ if [[ … ]];
then … elif [[ … ]]; then … else … fi 13
Controllo di flusso — case $ case $var in 1)
<comando 1> ;; 2-5) <comando 2> ;; *) <comando default> ;; esac 14
Ciclo for $ for f in <seq>; do … done
15
Sostituire i cicli con find e xargs $ find .
-iname \*.txt -exec chmod go-wx {} \; $ find /tmp -iname \*.sh | xargs grep sudo 16
Farsi ritornare il risultato di un comando $ var=` <comando
1> ` $ var=$( <comando 2> ) 17
Input dall’utente Caserecci: $ echo -n "Dimmi il tuo nome:
"; read var Un po’ più carini: $ var=`whiptail --inputbox "Dimmi il tuo nome:" \ 0 0 3>&1 1>&2 2>&3` 18
Non sempre comodo… Spesso e volentieri si preferisce chiedere input
all’utente tramite gli argomenti dello script. $ bash mioscript.sh argomento1 argomento2 19
Argomenti $0 Percorso e nome dello script $1 Primo argomento
$2 Secondo argomento … $@ Tutti gli argomenti $# Il numero degli argomenti passati 20
Lavorare con i file $ dirname $var $ basename $var
.ext 21