LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-28-2013, 03:46 PM   #1
bryanh
LQ Newbie
 
Registered: Feb 2013
Location: Dallas, TX
Distribution: Use Fedora now KDE
Posts: 4

Rep: Reputation: Disabled
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
 
Old 03-28-2013, 07:18 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,898

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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.
 
Old 03-29-2013, 12:09 AM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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

Last edited by shivaa; 03-29-2013 at 12:16 AM. Reason: Reformatting
 
Old 03-29-2013, 08:04 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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

Last edited by David the H.; 03-29-2013 at 08:08 AM. Reason: minor corrections
 
Old 03-29-2013, 02:31 PM   #5
bryanh
LQ Newbie
 
Registered: Feb 2013
Location: Dallas, TX
Distribution: Use Fedora now KDE
Posts: 4

Original Poster
Rep: Reputation: Disabled
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...
 
Old 03-30-2013, 10:50 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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
 
Old 03-31-2013, 11:37 PM   #7
bryanh
LQ Newbie
 
Registered: Feb 2013
Location: Dallas, TX
Distribution: Use Fedora now KDE
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thumbs up 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.
 
Old 04-02-2013, 06:58 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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

Last edited by David the H.; 04-02-2013 at 07:15 AM. Reason: addendum
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading character by character to dynamically allocated array. RollingKeg Programming 3 11-07-2012 08:44 PM
[SOLVED] Number of backslash character to use in grep luvshines Programming 12 10-04-2011 12:57 AM
[SOLVED] reading unix file permissions into a bash array for processing dazdaz Programming 11 12-06-2010 04:21 PM
Weird backslash character in NFS path names mocambo Linux - Networking 2 09-05-2009 08:54 PM
Processing XML file (reading records) lourencojunior Linux - Newbie 2 06-28-2008 02:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration