LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   way to overcome a backslash / character when reading file and processing (https://www.linuxquestions.org/questions/linux-newbie-8/way-to-overcome-a-backslash-character-when-reading-file-and-processing-4175455943/)

bryanh 03-28-2013 02:46 PM

way to overcome a backslash / character when reading file and processing
 
Had a simple script to create barcodes out of a file database

#!/bin/bash

for idnum in `cat database`

barcode -b $idnum -o $idnum.ps #gnu barcode 0.98

my problems is everything worked find until I discovered that a couple of records had / character as last character and is a true id number.

eg WOW00001
WOW00002/
WOW00003

creates barcodes only for 1st and 3rd record

bigrigdriver 03-28-2013 06:18 PM

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.

shivaa 03-28-2013 11:09 PM

If you can do "cat database", then you can use sed or awk to remove the trailing slash from such records. For instance, you can:
Code:

#!/bin/bash
sed -e 's/\/$//' database > newdatabase

while read -r idnum; do
barcode -b $idnum -o $idnum.ps #gnu barcode 0.98
done < newdatabase

\rm newdatabase


David the H. 03-29-2013 07:04 AM

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.

How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?
http://mywiki.wooledge.org/BashFAQ/001

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.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

bryanh 03-29-2013 01:31 PM

Need that backslash encoding barcode helped but still stuck
 
Read the reply on encoding and was working with that all morning

I basically read to enclose with ``data/'' and so if I do

barcode -b ``data/'' -o data.ps
Here is an example command line:

[root@MotherShip batch]# barcode -b ``kdsafkjasdkfj/'' -o kdsa.ps


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...

I did try mapfile example and failed too...

Thanks everyone for the tips so far...

David the H. 03-30-2013 09:50 AM

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'm trying to put a command in a variable, but the complex cases always fail!
http://mywiki.wooledge.org/BashFAQ/050

bryanh 03-31-2013 10:37 PM

Thank you filename expansion solved the problem
 
Thank you very much

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.

One example site which I feel has helped me learn alot is
Alien's bash tutorial
http://subsignal.org/doc/AliensBashTutorial.html

TLDP.org I have followed some and some others.

Again I appreciate the help when I was stumped and agree that this site should not just do someone elses homework.

David the H. 04-02-2013 05:58 AM

Glad it's working out for you.

I usually recommend the http://mywiki.wooledge.org/BashGuide (and the whole site, really, as you can probably tell ;)) and Bash Hackers as the best overall resources on shell use.

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.

http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression


All times are GMT -5. The time now is 01:33 AM.