Usa "*" nello script di shell

Oct 14 2020

Sto cercando di eseguire un esempio di comando:

sudo mv /home/vaishnavi/Downloads/*.ttc /home/vaishnavi/Downloads/*.ttf /usr/local/share/fonts/ms_fonts/

Funziona bene nel terminale, ma quando provo ad aggiungerlo in uno script di shell ( .sh) ottengo un errore che dice:

mv: cannot stat '/home/vaishnavi/Downloads/*.ttf': No such file or directory

Semplicemente, voglio spostare tutti i file di .ttfe .ttcdigitare in /usr/local/share/fonts/ms_fonts/, in questo caso. Francamente, non sono così esperto di script di shell. Per favore spiegami cosa ho fatto di sbagliato e anche un'alternativa per ottenere i risultati attesi.

Il mio script di esempio:

sudo mv /home/vaishnavi/Downloads/*.ttc /home/vaishnavi/Downloads/*.ttf /usr/local/share/fonts/ms_fonts/  
...

exit 0

Risposte

3 glennjackman Oct 14 2020 at 19:32

Riceverai questo errore se non ci sono file * .ttf lì. Per impostazione predefinita, se nessun file corrisponde a un pattern glob, il pattern stesso rimane nel comando. Disattivalo con l' nullglobopzione bash shell

Per essere un po 'più sicuro, verifica che ci siano effettivamente alcuni file da spostare:

#!/usr/bin/env bash
...
shopt -s nullglob
# store files in an array
files=( /home/vaishnavi/Downloads/*.{ttc,ttf} )
# and if the array is not empty, move the files.
if (( ${#files[@]} > 0 )); then sudo mv -t /usr/local/share/fonts/ms_fonts/ "${files[@]}" 
fi