Problem wth spec chars in script

Hello

I would like to make a script which will get the line from file list (ex. passkey) and put it into further processing.
The main problem is that lines, in text file contains all specials characters, and whitespaces too, as these that was used as a extremmaly-safe passwords. I have written my passwords to .7z files on long list, but I can't find which one is propper for specific .7z file. So i am expecting "password" string and getting it line-by-line from passkey list and later each line into next 7z spawns.

Unfortunately bash stopping the run when first special character occurs on the list, and script trying to load it, going to problem

So I have created two scripts:
which I am calling ~/whiler /path/file.7z

  1. 'whiler'
 1 while read line ; do
 2 echo "$line"
 3 time  expect ~/2.exp $1 "$line"
 4
 5 done<~/passkey.txt
 
  1. and the expect cmdfile:
  1 #!/usr/bin/expect
 2 set filename [lindex $argv 1]; # Grab the first command line parameter
 3 set passkey [lindex $argv 2]; # Grab the 2nd command line parameter
 4 set timeout 1
 5 spawn 7z t $filename
 6 expect "password:"
 7 send "$klucz\r";
 8 interact
 

I also tried to make password list in doublequotes (") each line.

What can I do to use strings from file bypassing the injection bash ? Or maybe my scripts are bad?

Thanks for any help

Is the ASCII 13 \r part of the password, or is this running on windows?

If it really is part of the actual password, you may get better results by using printf, maybe like this:

var=$(printf "%s%c" "\$klucz" 13)

Then pass "$var" to the second script as the third arg. Also assuming the leading $ is part of the password. Leading $ characters are a problem for shell.

Thank you for your reply,

Hmmm, I have thinked that is to use as RETURN (ENTER) key. That is not a pasword - all password is in $klucz or $passkey variable and should came from shell script.

I am running that on ubuntu studio 16.04 where I have installed expect trough apt - version 1+

My main problem are marks like @, !, ;,\, and similar (really - i dont know which any others :() - which used in bash console the have functions.

Your answer is somewhat evasive.

Two ways:

  1. use the printf method to embed punctuation and other odd characters. Unless you are using windows do not put \r in anything. expect gets a return from bash reading the script -- and it is a newline, \n

  2. you can escape characters ( \ escape in front of each character, so \\ is read by the shell as a backslash, not escape ). The shell takes them as literal characters, not some shell command -- example \*\# is an asterisk and an octothorpe (pound sign). Not a wild card and not a start of a comment.

Thank You Jim for your help - and sorry for my late reply. The problem is still actual for me.

So, I suppose, that only proper way for me will be the 1st method. Due to diversity of the passkeys it would be easier than putting backslashes in 2000-marks passkey's for example. The lenght of the keys can differ, and the special chars qty is also random.

About the method you mentioned:

  1. I would like to use only in linux, and on linux I also can prepare passlist (unix-type line endings and non viible characters on passlist).

  2. If that method (printf) could help - maybe 'expect' will not be essential?

I have tried using expect due to problems with using 7z x -p$klucz (or $passkey) archive.7z
The $ mark is only for calling a variable. That is possible, that on the begginning will be Dollar sign - $ .

Maybe your solution would allow me to use passkeys without bash-processing it in native 7z commands? So, if I can use it as a full text in script - 7z x -p$passkeyfromprintf

Unfortunately - I am starting with bash programming - and I really do not have idea how to use your snippet. Would you like to show me any example how to start using this ?

So, if I would use it my simpler script: (could you please help me with that one?)
"7z t" - 7z TEST, which wll exit when the passkey is bad.

2var=$(printf "%s%c" "\$klucz" 13)
while read $klucz 
do  7z t -p$2var archive.7z
echo $2var
done<passkeylist.txt

Is that should work?
regards