Well, call me a noob, but I tried John1024's suggestion and it gave me an error, so I tried figuring it out on my own. I made a file named readline.sh
that contained the following:
#!/bin/bash
file=$1 # $1 contains the argument passed
> line.txt # create/empty line.txt
IFS=$'\n' # set Internal Field Separator to '\n'
for line in `cat $file` # assign each line in $file to $line
do
echo $line >> line.txt # print $line to line.txt
# this one's tricky because you need to pipe just the line
# (without a '\n') into xxd and then print it to line.txt
echo `printf "%s" "$line" | xxd -p` >> line.txt
done
cat line.txt # we want to see the contents of line.txt now
Now to run it I called ls | ./readline.sh
and it output the following:
bar
626172
baz
62617a
foo
666f6f
line.txt
6c696e652e747874
readline.sh
726561646c696e652e7368
Now, I want to point out that this script doesn't have any way of checking that $1 even exists, and there's probably other issues with it, but, assuming I understand your question right, it seems the essence of the solution is to pipe the output of ls
line by line into xxd -p
. All the extra fluff was a) for me to make sure it worked before posting it and b) to show the principle in action.
\n
in the file name. Not sure whetherls
supports Unicode.