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
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
280
Introduzione a Python – Funzionalità di base e strutture dati
bglug
1
290
Linux dalla Console - quarta serata
bglug
0
210
Linux dalla Console - seconda serata
bglug
1
290
Linux dalla Console - prima serata
bglug
1
360
Other Decks in Programming
See All in Programming
Railsアプリケーションと パフォーマンスチューニング ー 秒間5万リクエストの モバイルオーダーシステムを支える事例 ー Rubyセミナー 大阪
falcon8823
5
1.1k
#QiitaBash MCPのセキュリティ
ryosukedtomita
1
990
第9回 情シス転職ミートアップ 株式会社IVRy(アイブリー)の紹介
ivry_presentationmaterials
1
280
イベントストーミング図からコードへの変換手順 / Procedure for Converting Event Storming Diagrams to Code
nrslib
2
650
WebViewの現在地 - SwiftUI時代のWebKit - / The Current State Of WebView
marcy731
0
110
Result型で“失敗”を型にするPHPコードの書き方
kajitack
5
610
Kotlin エンジニアへ送る:Swift 案件に参加させられる日に備えて~似てるけど色々違う Swift の仕様 / from Kotlin to Swift
lovee
1
260
チームで開発し事業を加速するための"良い"設計の考え方 @ サポーターズCoLab 2025-07-08
agatan
1
330
Composerが「依存解決」のためにどんな工夫をしているか #phpcon
o0h
PRO
1
250
ISUCON研修おかわり会 講義スライド
arfes0e2b3c
1
430
A full stack side project webapp all in Kotlin (KotlinConf 2025)
dankim
0
110
Deep Dive into ~/.claude/projects
hiragram
14
2.5k
Featured
See All Featured
Writing Fast Ruby
sferik
628
62k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
657
60k
Build The Right Thing And Hit Your Dates
maggiecrowley
36
2.8k
How GitHub (no longer) Works
holman
314
140k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
950
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Producing Creativity
orderedlist
PRO
346
40k
Product Roadmaps are Hard
iamctodd
PRO
54
11k
Practical Orchestrator
shlominoach
188
11k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
680
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.7k
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