Aquí hay una respuesta a la pregunta Y (ignorando la pregunta X ), inspirada en el intento del OP:
#!/bin/bash
LC_COLLATE=C
while read ls_out
do
extra=0
perms=0
for i in {1..9}
do
# Shift $perms to the left one bit, so we can always just add the LSB.
let $((perms*=2))
this_char=${ls_out:i:1}
# If it's different from its upper case equivalent,
# it's a lower case letter, so the bit is set.
# Unless it's "l" (lower case L), which is special.
if [ "$this_char" != "${this_char^}" ] && [ "$this_char" != "l" ]
then
let $((perms++))
fi
# If it's not "r", "w", "x", or "-", it indicates that
# one of the high-order (S/s=4000, S/s/L/l=2000, or T/t=1000) bits
# is set.
case "$this_char" in
([^rwx-])
let $((extra += 2 ** (3-i/3) ))
esac
done
printf "%o%.3o\n" "$extra" "$perms"
done
Lo anterior contiene algunos bashismos. La siguiente versión parece ser compatible con POSIX:
#!/bin/sh
LC_COLLATE=C
while read ls_out
do
extra=0
perms=0
for i in $(seq 1 9)
do
# Shift $perms to the left one bit, so we can always just add the LSB.
: $((perms*=2))
this_char=$(expr "$ls_out" : ".\{$i\}\(.\)")
# Lower case letters other than "l" indicate that permission bits are set.
# If it's not "r", "w", "x", or "-", it indicates that
case "$this_char" in
(l)
;;
([a-z])
: $((perms+=1))
esac
# If it's not "r", "w", "x", or "-", it indicates that
# one of the high-order (S/s=4000, S/s/L/l=2000, or T/t=1000) bits
# is set.
case "$this_char" in
([!rwx-])
: $((extra += 1 << (3-i/3) ))
esac
done
printf "%o%.3o\n" "$extra" "$perms"
done
Notas:
Uso:
$ echo drwxr-xr-x | chmod-format
0755
$ echo -rwsr-sr-x | chmod-format
6755
$ echo -rwSr-Sr-- | chmod-format
6644
$ echo -rw-r-lr-- | chmod-format
2644
$ echo ---------- | chmod-format
0000
Y sí, sé que es mejor no usar echocon texto que pueda comenzar -; Solo quería copiar el ejemplo de uso de la pregunta. Tenga en cuenta, obviamente, que esto ignora el carácter 0 (es decir, el d/ b/ c/ -/ l/ p/ s/ D) y el 10 ( +/ ./ @). Se supone que los mantenedores de lsnunca definirán
r/ Ro w/ Wcomo caracteres válidos en la tercera, sexta o novena posición (y, si lo hacen, deberían ser golpeados con palos ).
Además, acabo de encontrar el siguiente código, por cas , en
Cómo restaurar la propiedad predeterminada del grupo / usuario de todos los archivos en / var :
let perms=0
[[ "${string}" = ?r???????? ]] && perms=$(( perms + 400 ))
[[ "${string}" = ??w??????? ]] && perms=$(( perms + 200 ))
[[ "${string}" = ???x?????? ]] && perms=$(( perms + 100 ))
[[ "${string}" = ???s?????? ]] && perms=$(( perms + 4100 ))
[[ "${string}" = ???S?????? ]] && perms=$(( perms + 4000 ))
[[ "${string}" = ????r????? ]] && perms=$(( perms + 40 ))
[[ "${string}" = ?????w???? ]] && perms=$(( perms + 20 ))
[[ "${string}" = ??????x??? ]] && perms=$(( perms + 10 ))
[[ "${string}" = ??????s??? ]] && perms=$(( perms + 2010 ))
[[ "${string}" = ??????S??? ]] && perms=$(( perms + 2000 ))
[[ "${string}" = ???????r?? ]] && perms=$(( perms + 4 ))
[[ "${string}" = ????????w? ]] && perms=$(( perms + 2 ))
[[ "${string}" = ?????????x ]] && perms=$(( perms + 1 ))
[[ "${string}" = ?????????t ]] && perms=$(( perms + 1001 ))
[[ "${string}" = ?????????T ]] && perms=$(( perms + 1000 ))
He probado este código (pero no a fondo), y parece funcionar, excepto por el hecho de que no reconoce lo Len la sexta posición. Sin embargo, tenga en cuenta que, si bien esta respuesta es superior en términos de simplicidad y claridad, la mía es en realidad más corta (contando solo el código dentro del bucle; el código que maneja una sola -rwxrwxrwxcadena, sin contar comentarios), y podría hacerse aún más corto reemplazando
con .if condition; then …condition && …
Por supuesto, no debe analizar la salida dels .
stat. ¿Lo tienes? (Es una herramienta GNU, por lo que en su mayoría disponibles en Linux, no en Unix.)