0

Assume, I do archive several files with this functions:

gen_password () 
{
    gpg --gen-random 1 "$1" | perl -ne'
        s/[\x00-\x20]/chr(ord($^N)+50)/ge;
        s/([\x7E-\xDB])/chr(ord($^N)-93)/ge;
        s/([\xDC-\xFF])/chr(ord($^N)-129)/ge;
        print $_, "\n"'
}

archive () 
{
    ARCHIVE_NAME="$1"
    PASSWORD=$(gen_password 32)
    7za a -p"$PASSWORD" -mhe -- "$ARCHIVE_NAME" "$@"
    echo "Created 7z archive with password '$PASSWORD'"
}

This works well and I tried to upload encrypted archive on file sharing server.

So there is the script that uploads content of file to the server(source):

upload() 
{
    if [ $# -eq 0 ]; then echo "No arguments specified. Usage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md"; return 1; fi 
    tmpfile=$( mktemp -t transferXXX ); if tty -s; then basefile=$(basename "$1" | sed -e 's/[^a-zA-Z0-9._-]/-/g'); curl --progress-bar --upload-file "$1" "https://transfer.sh/$basefile" >> $tmpfile; else curl --progress-bar --upload-file "-" "https://transfer.sh/$1" >> $tmpfile ; fi; cat $tmpfile; rm -f $tmpfile;
}

So I'm trying to pipe the encrypted archive in naive way:

archive 1.rar pass.tar.gz d7432.png foo.7z | upload

But there is one problem - encrypted archive is unreacheable for upload and command exiting with no result.

So, the question is: how should I pipe the file to have it uploaded correctly?

  • it appears to me that your archive function is creating a filename (1.rar for example) and echos a string "Created 7z..."; that echoed string is the only thing that upload will see on its stdin. Maybe something like archive 1.rar ... && upload 1.rar ? – Jeff Schaller Feb 17 '16 at 20:21
  • @JeffSchaller seems like you're right. Is it possible to somehow pipe only name of resultiong foo.7z to upload function without deleting echo? – im_infamous Feb 17 '16 at 20:27
  • echo other stuff >&2; echo foo.7z – Jeff Schaller Feb 17 '16 at 20:29
  • @JeffSchaller unfortunately, still getting same result as initially. Seems like this functions could not be modularized(well, at least with my shell scripting skills) and I should consider upload handle the encryption too – im_infamous Feb 17 '16 at 20:38
  • well, upload() is getting stdin from your pipeline, but it seems to be expecting a parameter; either pass it a parameter or have it read the filename from stdin – Jeff Schaller Feb 17 '16 at 20:52
2

Since your upload() function is expecting a parameter ($1) to use as the archived filename, pass it along in your commandline:

archive foo.7z 1.rar pass.tar.gz d7432.png && upload foo.7z

If foo.7z is a variable parameter for archive() as well, simply pass the same variable to upload():

archive $archivename 1.rar pass.tar.gz d7432.png && upload $archivename

I would recommend the && glue, as you probably don't want to try to upload the archive file if the archive() function did not succeed.

Here is sample function for .bashrc:

share()
{
    ARCHIVE_NAME="$1"
    archive "$ARCHIVE_NAME" "$@" && upload "$ARCHIVE_NAME"
}
  • Seems like this works! Thanks a lot for your time! – im_infamous Feb 17 '16 at 21:41

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.