Bash-Script Problem: a=$(cat $1) [SOLVED]

Forum rules
Before you post please read how to get help
Post Reply
kaczoland
Level 1
Level 1
Posts: 3
Joined: Sat May 27, 2017 5:36 am

Bash-Script Problem: a=$(cat $1) [SOLVED]

Post by kaczoland » Mon Jun 05, 2017 9:46 am

Hi guys,

i've just started to learn a bit about Bash-Scripting and i've tried to make a script on my own but unfortunately it's not working as it should :/

Here is the script:

Code: Select all

 
#!/bin/bash
a=$(cat  "$1")
echo $a
a=${a//ä/ae}
a=${a//ü/ue}
a=${a//ö/oe}
a=${a//ß/ss}
echo $a
My intension is to replace every german additional character (äüöß) with the international letters(au,ue,oe,ss). The Input should be read from a .txt file, which should be the 1 parameter of this command. At the end of the user should see the content of the file but with the replaced characters.

My problem is that

Code: Select all

a=$(cat "$1") 
removes the free passages. So for Example the text file:
"Hello
World"

will be shown in the terminal by this command as "Hello World". Thats changing the style of the file but thats not my intesion. It should only replace the additional characters :(

Greets Kaczo :D
Last edited by kaczoland on Mon Jun 05, 2017 4:32 pm, edited 1 time in total.

User avatar
WharfRat
Level 20
Level 20
Posts: 10584
Joined: Thu Apr 07, 2011 8:15 pm

Re: Bash-Script Problem: a=$(cat $1)

Post by WharfRat » Mon Jun 05, 2017 10:32 am

Use echo "$a"

Code: Select all

[bill@XPS] ~/junk $ cat test
"Hello
World"
[bill@XPS] ~/junk $ a=$(cat test)
[bill@XPS] ~/junk $ echo "$a"
"Hello
World"
[bill@XPS] ~/junk $ echo $a
"Hello World"
[bill@XPS] ~/junk $ 
Image ImageImage

kaczoland
Level 1
Level 1
Posts: 3
Joined: Sat May 27, 2017 5:36 am

Re: Bash-Script Problem: a=$(cat $1)

Post by kaczoland » Mon Jun 05, 2017 10:51 am

Damn thx :)

lmuserx4849
Level 5
Level 5
Posts: 599
Joined: Wed Dec 17, 2014 2:55 am

Re: Bash-Script Problem: a=$(cat $1) [SOLVED]

Post by lmuserx4849 » Mon Jun 05, 2017 6:50 pm

May I make one suggestion. Use "read" rather than "cat".

Code: Select all

set -u   # unset variables are an error. Can help catch problems
declare inFile="${1}"
declare outFile="${HOME}/outputFile.txt"

# Does file exist. See  command line: "help test" or "man bash" for other tests
if [[ -e "${inFile}" ]]; then
  echo "ERROR: ${inFile} does not exist"
  exit 1  # get out
fi

while read -r line; do
  line="${}"                                 # do your substitutions here (like your example)
  #.... 
  echo "${line}" >> "${outFile}"   # append line to output file
done < "${inFile}"
Note: I normally used printf, but I used echo here to keep it simple.

Two great resources are:
Linux command line - His book is available for download, plus he has a very good tutorial online.

Unofficial Bash FAQ - Every bash question you can think of has been answered with examples. :-)

FYI: Get in the habit of always quoting bash variables, unless you have a good reason not too. See the google bash style guide. See bash manual on quoting: 3.1.2 Quoting

Post Reply

Return to “Scripts & Bash”