0

Write a shell script file_locker.sh to zip a file and protect such with a hashed password of a random generated PIN of specified length, while storing the PIN to a specified file.

#!/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

However, command line tells me like that:

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

I don't how to improve the zip command. please help !

0

you should use quotes in arguments...
and use $m instead of m in for loop

#!/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"

zip error: Invalid command arguments ..

you will get this if your argument has spaces in it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.