0

i have a file called 'file1.txt' that contain for exemple:

bibi.toto
jaja.bubu
vrtegbvtr.rvgtbtdtbvtd

And i want to echo some text with the var content(that contain the file1.txt lines) like that:

var=$(cat file1.txt)
echo 'hello there is $var' > final.sh

To get finaly:

final.sh content:

hello there is bibi.toto
hello there is jaja.bubu
hello there is vrtegbvtr.rvgtbtdtbvtd

The script must use busybox applets (awk, grep, sed, cat,...).

------------------------------------------

I have tried first with echo command but i get: hello there is $var even if i use double quotes (") nor {} and i can't use double quotes for echoing the content to the file because my text have some ' and " symbols.

I have also tried with sed like that:

var=$(cat file1.txt)

final.sh content:
hello there is pattern

sed "s/pattern/$var/g" final.sh

But i get: sed: unmatched '/' if i use: sed "s/pattern/\$var/g" final.sh i get: hello there is $var.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Rom
  • 31

5 Answers5

2

Try:

sed 's/^/hello there is /' file1.txt > final.sh

Or if you have to use a variable:

printf '%s\n' "$var" | sed 's/^/hello there is /' > final.sh

If you don't mind losing empty lines, you could also do:

IFS='
' # split on newline
set -o noglob # disable glob
printf 'hello there is %s\n' $var > final.sh # invoke split+glob

For your actual problem:

sed "s/.*/UPDATE ownership SET library_id = 'u-wl' where doc_id = '&';/" < file1.txt |
  sqlite "$PLAY_DB_DIR/library.db"

(note that it assumes the entries in the files don't contain ' or other special byte sequences in the sqlite language. If you can't get that guarantee, that would be a sqlite injection vulnerability at best).

Or if you need to output shell code to be executed later:

{
  echo 'sqlite "$PLAY_DB_DIR/library.db" << "EOF"'
  sed "s/.*/UPDATE ownership SET library_id = 'u-wl' where doc_id = '&';/" < file1.txt
  echo EOF
} > final.sh

Which on your sample gives:

sqlite "$PLAY_DB_DIR/library.db" << "EOF"
UPDATE ownership SET library_id = 'u-wl' where doc_id = 'bibi.toto';
UPDATE ownership SET library_id = 'u-wl' where doc_id = 'jaja.bubu';
UPDATE ownership SET library_id = 'u-wl' where doc_id = 'vrtegbvtr.rvgtbtdtbvtd';
EOF

Or if you have to run one sqlite command per query:

repl=$(cat << "EOF"
sqlite "$PLAY_DB_DIR/library.db" 'UPDATE ownership SET library_id = '\\''u-wl'\\'' where doc_id = '\\''&'\\'';'
EOF
)
sed "s@.*@$repl@" < file1.txt > final.sh

(this time in addition to a sqlite injection vulnerability, that becomes an arbitrary command execution vulnerability, if the file may contain single quote characters).

On your sample, that gives:

sqlite "$PLAY_DB_DIR/library.db" 'UPDATE ownership SET library_id = '\''u-wl'\'' where doc_id = '\''bibi.toto'\'';'
sqlite "$PLAY_DB_DIR/library.db" 'UPDATE ownership SET library_id = '\''u-wl'\'' where doc_id = '\''jaja.bubu'\'';'
sqlite "$PLAY_DB_DIR/library.db" 'UPDATE ownership SET library_id = '\''u-wl'\'' where doc_id = '\''vrtegbvtr.rvgtbtdtbvtd'\'';'
  • Thank you, i use the first or the second one command, but i have a problem with my text: ./sqlite $PLAY_DB_DIR/library.db "UPDATE ownership SET library_id = ''u-wl' where doc_id = 'pattern''";' according to this: https://unix.stackexchange.com/questions/32907/what-characters-do-i-need-to-escape-when-using-sed-in-a-sh-script when i set backslashs for $.*/[]^ symbols, i get an bad pattern error from sed. I don't know where is my error(s). Maybe that it could be easier with awk or tr maybe ? – Rom Oct 24 '18 at 09:13
  • So I'm going to do my "grumpy", but the sqlite command must remain the same as the one I initially noted. So the command should be: sed "s/.*/./sqlite $PLAY_DB_DIR/library.db "UPDATE ownership SET library_id = 'u-wl' where doc_id = '&';/" < $(cat $var) | final.sh because this command must to be writed into a shell file to be executed in Android environment. My one is good ? – Rom Oct 24 '18 at 09:40
  • @Rom, see edit, it's a bit pointless to run one sqlite command per query. Your one has issues with variable expansion inside double quotes, and the fact that there are too many / in your s/../../ command (use a separate delimiter like s@...@...@). – Stéphane Chazelas Oct 24 '18 at 09:51
  • Only one sqlite command must be executed at a time. A friend told me that it would be possible to do this action via the command if the use of sed became too complex. According to him, the command should be this: echo './sqlite $PLAY_DB_DIR/library.db "UPDATE ownership SET library_id = `'u-wl' where doc_id = '$var''";' > /folder/final.sh according to him since the variable contains in any case several lines, echo would then write as many lines as necessary by replacing $var with the following word contained in the variable. – Rom Oct 24 '18 at 11:11
  • He also pointed out to me that the double quotation (") should not be used, but only the single quotation (') in order to avoid any ambiguity with the many special carachters contained in the text to be dealt with. His control didn't work. My last request would be the command that would fit in a single line, and would therefore write one or more sqlite command lines in the.sh file. That's why I proposed to use awk or other Linux applets. Also, i don't know how the s@...@...@) delimiter works, it's the same as the backslash \ or not ? – Rom Oct 24 '18 at 11:11
  • See edit for a variant that calls one sqlite per query. – Stéphane Chazelas Oct 24 '18 at 11:31
  • Thanks for your efforts, after more than a dozen tests, it seems that the console in which my commands are executed does not like "EOF" very much, when executed by an unknown interpreter (I think it must be written in JAVA), I get "error "line: (last line): syntax error: unexpected end of file (expecting")". – Rom Oct 24 '18 at 21:49
  • Strangely enough I managed to get your last commands to work via adb shell with root access rights. I have seen on the internet that there is also the "EOD" option, but I have not found the difference between "EOF" and "EOD". It's amazing how much a shell can become a black cat. By any chance, is this your last method identical to the varname=(awk'{ print }' $var) command? – Rom Oct 24 '18 at 21:49
  • After many attempts, it turns out that your last command is working. The shell in which the script is to be executed is proving to be a little strange in the way it works with EOF, but basically, the code works as expected via adb (sh). Thank you very much for your effort. – Rom Oct 25 '18 at 06:15
1

How about

$ readarray -t X <file
$ IFS=$'\n'
$ echo "${X[*]/#/hello there is }"
hello there is bibi.toto
hello there is jaja.bubu
hello there is vrtegbvtr.rvgtbtdtbvtd
RudiC
  • 8,969
  • Thanks for your reply, but readarray isn't recognized on Android, and this applet isn't implemented on Busybox: https://busybox.net/downloads/BusyBox.html – Rom Oct 24 '18 at 08:58
  • @Rom, yes, readarray and ${x[*]/#/...} are bash-specific. It won't work in any other shell. – Stéphane Chazelas Oct 24 '18 at 09:31
0

ex1 :

for i in $(cat file.txt); do; echo "hello there is $i"; done

hello there is bibi.toto

hello there is jaja.bubu

hello there is vrtegbvtr.rvgtbtdtbvtd.

here is an example with loop for!

ex2:

while read linha; do echo "hello there is $linha"; done < file.txt

hello there is bibi.toto

hello there is jaja.bubu

hello there is vrtegbvtr.rvgtbtdtbvtd

so it gets each line of file file.txt

noob
  • 22
0

while read -r var; do echo hello this is $var; done < file1.txt > final.sh should do what you want. This assumes the ash shell, which I believe is the one in busybox. This uses the read builtin to read the file a line at a time into var.

An equivalent bash implementation would look like

while read -r; do echo hello this is ${REPLY}; done < file1.txt > final.sh

which utilizes bash's read builtin.

james
  • 46
  • Thanks for your answer, according to your exemple, i don't know if read is a bash specific or a command like cp or egrep for exemple. I think that if i must use your exemple, i need to use double-quote " to matching with expension command. Personally i never used echo command without quote ' or double quote ". Thanks for your effort. – Rom Oct 25 '18 at 06:24
  • read is a bash builtin. ash also has a read builtin. The syntax is slightly different between the shell.s – james Oct 25 '18 at 09:59
0

Thanks everyone for having help me, especially @Stéphane Chazelas and @noob. My question can to be closed, the command from Stéphane basically works but the shell in wich the script must to be works is pretty strange (problem with EOF)..

As the first command exemple from noob is a little bit simple, it works. I have used my vars like that:

for i in $(cat $file1); do echo "text blabla $i blabla" >> $file2; done

Thanks again everyone.

Rom
  • 31