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

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
kaczoland

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

Post 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
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
WharfRat

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

Post 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 $ 
kaczoland

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

Post by kaczoland »

Damn thx :)
lmuserx4849

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

Post 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
Locked

Return to “Scripts & Bash”