su -c: create 7z with data from stdin and prompt for password

zackeee
2014-02-09
2014-03-01
  • zackeee
    zackeee
    2014-02-09

    Hi there,

    if the following script

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    #!/bin/bash
    
    # create 7z encrypted file
    if ! ( cat <<EOF | 7z -si -p -mhe a myfile.txt.7z
    this is my secret
    file!!!
    EOF
    ); then
        echo "Sorry... Failed.................."
    fi
    

    is run with
    su -c './script.sh'
    then everything goes wrong:

    The session the is like this:

    7-Zip 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18
    p7zip Version 9.20 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,2 CPUs)
    Creating archive myfile.txt.7z
    
    Enter password (will not be echoed) :Verify password (will not be echoed) :
    
    Error:
    password verification failed
    Sorry... Failed..................
    



    As you can see, the session does not wait for me to enter the password. It just skips right over that.
    Can someone point out what is going wrong here? How can I fix this?






    Just note that without "su -c"... the script works perfectly.
    So no problems, when run with
    ./script.sh

    Then the session goes correctly -- as follows:

    7-Zip 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18
    p7zip Version 9.20 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,2 CPUs)
    Creating archive myfile.txt.7z
    
    Enter password (will not be echoed) :
    Verify password (will not be echoed) :
    Compressing  [Content]
    
    Everything is Ok
    
     
    Attachments
  • Tom
    Tom
    2014-03-01

    Instead of asking 7zip to prompt for the password, just let bash do it, like this:

    read password
    7z -si -p $password -mhe a myfile.txt.7z

    I can't nicely format the above code cuz there are no rich text buttons, and nothing in the "formatting help" works from within Linux. Silly forum.

     
    Last edit: Tom 2014-03-01