•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 392,094 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,851 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser:
Views: 11606 | Replies: 27
![]() |
•
•
Join Date: May 2005
Posts: 215
Reputation:
Rep Power: 4
Solved Threads: 0
•
•
•
•
Originally Posted by gritty
I copied it character for character, then had someone else proof-read it. Unless I hit Ctl-C it keeps on adding spaces.
I must be dense, I do not think I am understaning what you are saying(sorry).
I am showing you exactly how it is working on my machine, it seems to stop after it pads the file to the 150 characters I specified
shane@mainbox shane $ echo "some characters" >file #this line creates a new file with some characters shane@mainbox shane $ ./pad #this runs the script enter the name of the file you want to pad file enter the total amount of characters you need in the file 150 shane@mainbox shane $ wc -m file 150 file
•
•
Join Date: May 2005
Posts: 215
Reputation:
Rep Power: 4
Solved Threads: 0
•
•
•
•
Originally Posted by gritty
I just figured it out. The wc -m command also adds the filename (at least in the UNIX/Linux version I've tried it on) so I need to pipe it to awk and strip the filename away so I have just the number. I think that was the only issue. I'll post my findings
I tried for a long time to strip the file name off of the output of (wc -m file_name) with either sed or tr , it was not working. you can do the following
wc -m file_name | sed 's/file_name/ /'
if not for the variable issue, you could also use tr command to strip it
wc -m file_name | tr 'file_name' ' '
for either of those above commands to work, you need to place a variable in place of file_name.
I got frustrated and could not get the file name stripped off of the ouput wc -m, so I went with this method
while [ "`wc -m $file`" != "`echo "$total $file"`" ]
•
•
Join Date: May 2005
Posts: 215
Reputation:
Rep Power: 4
Solved Threads: 0
do you mind showing me what you have on your script, it would give me a better idea of what you are doing.
edit added later//
I know why I was not able to use variables with sed or tr. The problem was using single quotes. Using double or no quotes works
you could also use sed like this
edit added later//
I know why I was not able to use variables with sed or tr. The problem was using single quotes. Using double or no quotes works
echo "enter the file name you want to pad"
read FILE
echo "enter the amount of characters you would like in the file"
read TOTAL
while [ `wc -m $FILE | tr $FILE ' '` -lt $TOTAL ]
do echo "" >> $FILE
doneyou could also use sed like this
while [ `wc -m $FILE | sed "s/$FILE/ /"` -lt $TOTAL ]
•
•
Join Date: Jul 2005
Posts: 23
Reputation:
Rep Power: 4
Solved Threads: 0
no, I put the "`" in there. I even tried to clear out testfile (the filename I'm testing with) and run it the same way you are but no luck. Here is exactly what I have in the script:
#!/bin/bash
echo "enter the name of the file you want to pad"
read file
echo "enter the total amount of characters you need in the file"
read total
while [ "`wc -m $file`" != "`echo "$total $file"`" ]
do echo "" >> $file
done
I believe it is exactly the same as your.
#!/bin/bash
echo "enter the name of the file you want to pad"
read file
echo "enter the total amount of characters you need in the file"
read total
while [ "`wc -m $file`" != "`echo "$total $file"`" ]
do echo "" >> $file
done
I believe it is exactly the same as your.
•
•
Join Date: May 2005
Posts: 215
Reputation:
Rep Power: 4
Solved Threads: 0
I think i see the problem. if you choose a number of characters that is less then the file already has, it will run forever. useing this script below should work better
#!/bin/bash
echo "enter the file name you want to pad"
read FILE
echo "enter the amount of characters you would like in the file"
read TOTAL
while [ `wc -m $FILE | tr $FILE ' '` -lt $TOTAL ]
do echo "" >> $FILE
done![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
- Korn Shell Script for deleting files older than 2 months (Shell Scripting)
Other Threads in the Shell Scripting Forum
- Previous Thread: Regex for password
- Next Thread: refering to individual fields in separate lines


Linear Mode