0

So, I'm new at using bash script, but I am an experienced programmer in Java. I am trying to evaluate a string, by having it go through a loop that looks at each of its characters. It then needs to replace all whitespace with a hyphen character ("-"). Here's my code:

for a in "${newdirectory[@]}"; do
str="LDAUU_"
str+=$a
echo $str | awk -v ORS="" '{ gsub(/./,"&\n") ; print }' | \
while read char
do
if [[ $char == "whitespacecharacter" ]]
then
str+="-"
else
str+=$char
fi
done

The newdirectory variable is an array of user input, which was initially a single variable that was separated into the newdirectory array using the delimiter ",". The for loop also continues, but it's irrelevant to this section of the script. Also, I would prefer not to restructure my code completely. I need something that just evaluates the "char" variable in the while loop as a whitespace character. That's it. Thank you.

  • Why not do the replacement in Awk*, which is designed for that sort of thing, instead of stepping through each character in bash? (*Or anything else, but since you're already using Awk...) – Jordan Running Oct 07 '14 at 22:58
  • As I said, I haven't used Bash. I don't know even what Awk is- I copied that line of code from another script. What could be a piece of code I could use to serve my purpose then? – Nathaniel Weissinger Oct 07 '14 at 23:19
  • `echo "$str" | tr ' ' -`. Search for questions like this on SO there are approximately a million. – Etan Reisner Oct 07 '14 at 23:24
  • That code worked, but I'd like to save that printed out line to a variable. Any simple way to do that? – Nathaniel Weissinger Oct 07 '14 at 23:35
  • `var=$( echo "$str" | tr ' ' - )` ...but Google would've given a faster answer ;) – bryn Oct 07 '14 at 23:48
  • Escaping protects against syntactical parsing. If you're letting your data being parsed as syntax, you're doing things dangerously wrong. Do things correctly, and there's no need to worry about escaping -- your data stays data, instead of trying to become syntax. – Charles Duffy Oct 08 '14 at 01:11
  • Anyhow -- don't **ever** use `echo $str`; being an unquoted expansion, that string-splits and glob-expands your string. `echo "$str"` is more correct, though it's _still_ wrong due to echo's limitations (try if your string is `-E` or `-n`). `awk <<<"$str"` is still better than that... but of course, the best answer is not to use `awk` here at all. – Charles Duffy Oct 08 '14 at 01:13
  • By the way -- future readers of this question might find http://stackoverflow.com/questions/301039/how-can-i-escape-white-space-in-a-bash-loop-list also of interest. – Charles Duffy Oct 08 '14 at 01:17
  • Thank you, all of you. I do not have any particular concern as to whether my code is perfect or not; this is a small program that copies a file multiple times and edits a specific variable in the file with a user input. This was the only thing that I had trouble with, so it's not as if I want to do something "correctly". I just want it to work. Thanks again! – Nathaniel Weissinger Oct 09 '14 at 04:08

2 Answers2

3

With bash, you can do it all at once with parameter substitution and pattern matching

str="i am full of spaces"
dashes="${str//[[:blank:]]/-}"
echo "$dashes"                  # i-am-full-of-spaces

Aside from the manual (referenced above) the BashGuide and BashFAQ are good resources for bash programming.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Thank you! This is almost exactly what I wanted. I do not have any particular concern as to whether my code is perfect or not; this is a small program that copies a file multiple times and edits a specific variable in the file with a user input. This was the only thing that I had trouble with. Thanks again! – Nathaniel Weissinger Oct 09 '14 at 04:11
3

I hear you, you don't want to "restructure your code completely". But changing your 5-line for/while imbrication for something a lot more readable won't qualify as "complete restructure".

It's really simple in bash to replace all spaces with a dash , given a variable str, just do:

str=${str// /-}

That will replace all occurences of ' ' with '-' .

People tend to use auxiliary tools like tr, awk and sed where bash can do the same job faster and with cleaner code, check out : http://wiki.bash-hackers.org/syntax/pe

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Gui Rava
  • 448
  • 4
  • 6
  • This answer was almost exactly what I wanted. Thank you! And you understood what I was saying; I do not have any particular concern as to whether my code is perfect or not; this is a small program that copies a file multiple times and edits a specific variable in the file with a user input. This was the only thing that I had trouble with. Thanks again! – Nathaniel Weissinger Oct 09 '14 at 04:10