Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Linux dalla Console - terza serata
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
BgLUG
April 05, 2017
Programming
1
350
Linux dalla Console - terza serata
Bash scripting
BgLUG
April 05, 2017
Tweet
Share
More Decks by BgLUG
See All by BgLUG
Introduzione a Python – Ecosistema Python e usi specifici
bglug
1
290
Introduzione a Python – Funzionalità di base e strutture dati
bglug
1
300
Linux dalla Console - quarta serata
bglug
0
220
Linux dalla Console - seconda serata
bglug
1
300
Linux dalla Console - prima serata
bglug
1
370
Other Decks in Programming
See All in Programming
CSC307 Lecture 09
javiergs
PRO
1
840
AI時代のキャリアプラン「技術の引力」からの脱出と「問い」へのいざない / tech-gravity
minodriven
21
7.2k
AIエージェントのキホンから学ぶ「エージェンティックコーディング」実践入門
masahiro_nishimi
5
460
登壇資料を作る時に意識していること #登壇資料_findy
konifar
4
1.1k
AI によるインシデント初動調査の自動化を行う AI インシデントコマンダーを作った話
azukiazusa1
1
730
360° Signals in Angular: Signal Forms with SignalStore & Resources @ngLondon 01/2026
manfredsteyer
PRO
0
130
なるべく楽してバックエンドに型をつけたい!(楽とは言ってない)
hibiki_cube
0
140
React 19でつくる「気持ちいいUI」- 楽観的UIのすすめ
himorishige
11
7.4k
CSC307 Lecture 03
javiergs
PRO
1
490
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
570
例外処理とどう使い分ける?Result型を使ったエラー設計 #burikaigi
kajitack
16
6.1k
MUSUBIXとは
nahisaho
0
130
Featured
See All Featured
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
75
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
320
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
270
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
0
2.3k
YesSQL, Process and Tooling at Scale
rocio
174
15k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
57
50k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
220
How to build a perfect <img>
jonoalderson
1
4.9k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Between Models and Reality
mayunak
1
190
Visualization
eitanlees
150
17k
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