Page 1 of 1

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

Posted: Mon Jun 05, 2017 9:46 am
by kaczoland
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

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

Posted: Mon Jun 05, 2017 10:32 am
by WharfRat
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 $ 

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

Posted: Mon Jun 05, 2017 10:51 am
by kaczoland
Damn thx :)

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

Posted: Mon Jun 05, 2017 6:50 pm
by lmuserx4849
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