problema zip en el script de shell. ¿Por qué está mal?

problema zip en el script de shell. ¿Por qué está mal?

Escriba un script de shell file_locker.sh para comprimir un archivo y protegerlo con una contraseña hash de un PIN generado aleatoriamente de una longitud especificada, mientras almacena el PIN en un archivo específico.

#!/bin/bash
file_locker(){

m=$1
for ((i=1;i<=m;i++));
do
num=($(( 0 + RANDOM % 9 )))

echo -n "$num"

done > $3

mypin=$(echo $3)
echo pin $mypin

myhash=$(echo $mypin | sha256sum)
echo hash $myhash

zip -P $myhash $2.zip -r $2 && rm $2

}
file_locker $1 $2 $3

Sin embargo, la línea de comando me dice así:

zip error: Invalid command arguments (cannot write zip file to terminal)

No sé cómo mejorar el comando zip. por favor ayuda !

Respuesta1

deberías usar comillas en los argumentos...
y usar $m en lugar de m en el bucle for

#!/bin/bash
file_locker(){

m="$1"
for ((i=1;i<=$m;i++));
do
num=($(( 0 + RANDOM % 9 )))

echo -n "$num"

done > "$3"

mypin=$(echo "$3")
echo pin "$mypin"

myhash=$(echo "$mypin" | sha256sum)
echo hash "$myhash"

zip -P "$myhash" "$2".zip -r "$2" && rm "$2"

}
file_locker "$1" "$2" "$3"

error zip: argumentos de comando no válidos..

Obtendrás esto si tu argumento tiene espacios...

información relacionada