Em csh , você pode usar um loop foreach para percorrer todos os subdiretórios dos diretórios no diretório atual:
foreach i (*/*/)
set g='echo $i | cut -d/ -f1' # slash-delimited, select first field
set obs='echo $i | cut -d/ -f2' # slash-delimited, select second field
echo $g $obs
end
O bash equivalente é um loop for . É claro que você pode usar cut como acima, mas bash também tem um recurso elaborado de Expansão de Parâmetro , então você não precisa chamar nada externo:
for i in */*/; do
g=${i%%/*} # cut everything from the first slash
obs=${i#*/} # cut everything until first slash
obs=${obs%/} # cut slash at the end
echo $g $obs
done
Substitua a linha echo pelo que você quiser com as variáveis.
Exemplo de execução
% é o prompt csh , $ o bash um.
% tree
.
├── IC0499
├── IC7216
│ └── 2222
├── NGC2201
│ ├── 2221
│ ├── 7019
│ └── note
└── NGC7169
7 directories, 1 file
% foreach i (*/*/)
? set g='echo $i | cut -d/ -f1'
? set obs='echo $i | cut -d/ -f2'
? echo $g $obs
? end
IC7216 2222
NGC2201 2221
NGC2201 7019
$ for i in */*/; do g=${i%%/*}; obs=${i#*/}; obs=${obs%/}; echo $g $obs; done
IC7216 2222
NGC2201 2221
NGC2201 7019
Citando o link :
Please note that
cshwas popular for many innovative features butcshhas never been as popular for scripting. If you are writing system level rc scripts avoid usingcsh. You may want to use/bin/shfor any scripts that might have to run on other systems.