Just to add a few scripting tips for you:
1.
/bin/sh forces your shell to interpret the script in posix-compliant mode. This means any advanced shell features will be unavailable. Since you're using Linux, and pretty much all distributions have bash installed, then you really should use
/bin/bash instead. Only use sh if you need portability to systems without bash installed.
2.
Code:
cat file2 | while read printers
do
(commands)
done
Useless Use Of Cat! You can feed the loop with a simple file redirection instead. More complex commands can be handled with
process substitution (which is one of those bash-only features I mentioned).
Code:
while read printers ; do
(commands)
done <file
while read printers ; do
(commands)
done < <( tr -d '\r' <file )
This also avoids the common pitfall of the loop running in a subshell.
http://mywiki.wooledge.org/BashFAQ/024
3.
Code:
echo $printers
grep $printers file1 > $printers
Always double-quote variable expansions, unless you want word-splitting to occur. It's vital in scripting to understand how the shell handles arguments and whitespace, so carefully read these three links:
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
Code:
echo "$printers"
grep "$printers" file1 > "$printers"
When in doubt, quote!
4.
Finally, think a bit about the formatting of your script. Good, consistent use of indenting and whitespace helps make your code easier to read and to debug.
http://mywiki.wooledge.org/BashGuide...es#Readability
I also personally recommend putting your
do and
then keywords on the same line as the start of the loop. It helps keep down clutter and makes seeing the where the loop starts and ends a bit clearer (when combined with proper indentation, of course). See my while-loop example above.