Reputation: 2934
I have a vi encrypted text file for storing the login details of DB. Now in my shell script I wanted to get the content say grep DB_NAME login_details.txt
.
How do I pass the vi encryption password to the grep command ?
Upvotes: 2
Views: 2042
Reputation: 326
thing=$(echo '1,$'|vim --cmd "set key=${password}" ${filename} -es|grep needle)
This will load vim with the password and file read in from variables that you set previously somehow, and then dump the entire file contents to stdout, and then grep for the string "needle" and if it exists, it will be stored as the $thing variable.
This example is a bad practice, you should use existing tooling to accomplish secure decryption.
Upvotes: 0
Reputation: 3791
I believe everything is explained on this vim wikia page: http://vim.wikia.com/wiki/Encryption. Read it whole (it warns you about when the file is actually encrypted, that the viminfo should be unset, etc...)
Especially is show how yo know the encryption used with :setlocal cm?
("show encryption method for the current file") and how to change it with :setlocal cm=...
too.
But this is not interactive "per say" ... but you can use command line equivalent to have vim do this from the command line (which then can be used in a script), adding commands to just print the relevant line(s)
If you meant vi instead of vim, you need to specify which OS it is on, and look at vi encryption: what algorithm has been used? This page shows 2 solutions depending on the type of OS used (And I'm quite sure there is a way to do the equivalent "on the fly", ie without having the decrypted file on disk... look for mcrypt -d --force ... (without specifying a destination file so it has to go to stdout. You need --force otherwise mcrypt refuses to output to stdout)
Upvotes: 2