way to overcome a backslash / character when reading file and processing
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
It is probably necessary to add the encoding you want to use. Not all encodings include special characters. The only one I've found that works with the / symbol in Code-39.
Try adding it to your command: -e Code-39
or some variation of code39. In Scribus barcode generator, it's shown as Code-39.
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
First of all, you don't really explain what you actually want to do. Do you want to remove the slash characters from the input, or convert them in some way, or what? bigrigdriver seems to have a handle on how to get the barcode reader to accept them, but I have no experience in that area myself.
If you want to remove the slashes from the input, a simple parameter substitution during variable expansion can take care of it.
Shivaa has also, indirectly, pointed out another of your problems: Don't Read Lines With For, which in this case is combined with a Useless Use Of Cat. When processing text from a file or a command you should always use a while+read loop.
Also remember to use the "-r" option with read to prevent it trying to interpret any backslash escapes in the input.
Oh yeah, the loop you posted is also missing the "do" keyword.
Another option you could use, if the input file has exactly one entry per line, is bash's mapfile built-in, which will load them into an array that can be safely processed by a for loop.
Code:
#!/bin/bash
mapfile -t idnums <database
for idnum in "${idnums[@]}"; do
barcode -b "$idnum" -o "$idnum.ps"
done
exit 0
Finally, QUOTE ALL OF YOUR VARIABLE EXPANSIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell and any possible globbing patterns expanded. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions when you need them.
the postscript file will have a barcode for string and have ending /
so then I played with sed to do this to my database file
#!/bin/bash
cp database dbase_$(date +%m_%d_%Y).bak
mv database base1
sed 's/^/``/' base1 > base2
sed -e "s/$/''/" base2 > database
rm -rf base1
rm -rf base2
for idnum in `cat database`
do
barcode -b "$idnum" -e "128b" -u "in" -g "3x.5+2.5+5.75" -o "$idnum".ps
ps2pdf "$idnum".ps "$idnum".pdf
done
But running script produces this output
[root@MotherShip batch]# ./script
barcode: ``kdsafkjasdkfj/''.ps: No such file or directory
GPL Ghostscript 9.05: **** Could not open the file ``kdsafkjasdkfj/''.pdf .
**** Unable to open the initial device, quitting.
barcode: ``ksdfkjasfjljs/''.ps: No such file or directory
GPL Ghostscript 9.05: **** Could not open the file ``ksdfkjasfjljs/''.pdf .
**** Unable to open the initial device, quitting.
As for the last post on following rules I agree but I am new to scripting
Although I have followed different web-pages everyone is different and then it comes down
to I have to have plenty of examples to follow till about 1/10 stays in my brain...
If your different-congratulations better job security...
As I mentioned before, please use ***[code][/code]*** tags around your code and data!
I just installed barcode to test it out, and that helped me determine the real problem. It has nothing to do with the program itself, but with the output file pattern. The loop is trying to create an output file with the same name as the input string, but it can't do it because the slash character is the directory separator, and illegal in filenames. The program tries to find a directory named with that number and fails.
So all you have to do is use a parameter substitution to remove them, but only from the filename expansion.
Code:
# With a traditional read loop:
while read -r idnum; do
barcode -b "$idnum" -o "${idnum//\/}.ps"
done <database
# Or with an array:
mapfile -t idnums <database
for idnum in ${idnums[@]}"; do
barcode -b "$idnum" -o "${idnum//\/}.ps"
done <database
In any case there should almost never be any need to add quotemarks to a string like you were trying to do. If you read the links I gave before on the shell's argument processing, then you should understand that trying to embed syntactical quotes in a string is usually doomed to failure.
( Not to mention that most programs do not use ``,'' for quotes, only " and '. The former is an old traditional style for quoting things in documentation, but is not generally found in actual code ).
Here's one more link that goes into detail on why you shouldn't try to put syntax elements inside parameters:
I did realize that the error was taking place when the script went to write the output file name.
Just did not think of filename expansion to solve problem
I did learn some new skills and if you are not familiar with enscript and pdftk program
they are really cool commands.
My goal was to collect data during an inventory and create a barcode of the item I inventoried. With enscript I can create a postscript file from a text input file and then use ghostscript to convert to pdf. Also pdftk allows you to combine, merge, remove pages plus many more features.
I was able to use the barcode program to place the barcode anywhere I wanted to on the page and then merge with a background pdf file with pdftk. This way my inventory page looked professional with a picture of our company logo and stuff and also included information on the item I inventoried (turn that data to ps with encript -Bp option...)
I appreciate your help David and all the others that responded.
What are some good sites to learn bash scripting from and also which are the better books to read as a beginner. I have to have plenty of examples.
When I started scripting 10 years ago, all I really had was the ABSG, LinuxCommand, the man page, and Google. They did the job in teaching the syntax, but it was Greg's wiki that finally taught me how to use it properly. I sure wish they were available when I was just starting out.
PS: I just took a quick glance at the Alien's tutorial. It looks like a good resource overall, but be advised that some of its examples are displaying older or less robust syntax forms. For example, it teaches the ancient, deprecated bash "$[..]" arithmetic brackets, and the old "[..]" test. The former shouldn't be used at all anymore (use the fully-portable "$((..))" arithmetic expansion instead), and the latter should generally be replaced by the safer and more powerful "[[..]]" and "((..))" for string and arithmetic comparisons respectively, as long as you are using bash, ksh, or another advanced shell.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.