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.

5 Answers 5

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'\'';'
10
  • 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: unix.stackexchange.com/questions/32907/… 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, 2018 at 9:13
  • @rom, see edit. Oct 24, 2018 at 9:27
  • 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, 2018 at 9: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@...@...@). Oct 24, 2018 at 9: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, 2018 at 11:11
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
2
  • Thanks for your reply, but readarray isn't recognized on Android, and this applet isn't implemented on Busybox: busybox.net/downloads/BusyBox.html
    – Rom
    Oct 24, 2018 at 8:58
  • @Rom, yes, readarray and ${x[*]/#/...} are bash-specific. It won't work in any other shell. Oct 24, 2018 at 9: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

4
  • be aware that your for loop iterates over words, not lines.
    – pLumo
    Oct 24, 2018 at 15:04
  • Ok I'll take another example!
    – noob
    Oct 24, 2018 at 16:05
  • Thanks for your reply, your first exemple works successfull with my stuff.
    – Rom
    Oct 25, 2018 at 6:12
  • 1
    @RoVo, worse, it iterates over the glob expansions of the words. Nov 1, 2018 at 13:10
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.

2
  • 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, 2018 at 6:24
  • read is a bash builtin. ash also has a read builtin. The syntax is slightly different between the shell.s
    – james
    Oct 25, 2018 at 9: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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .