0

I'm trying to create a function that allows me to send two variables to it. My code:

#!/bin/bash
function myfunc{
cat > file.xml << EOFcat
<hostdev mode='subsystem' type='usb' managed='yes'>
<source>
<vendor id='0x$1'/>
<product id='0x$2'/>
</source>
</hostdev>
EOFcat
}
trigger="191a:8003";
vID=$(lsusb | grep $trigger | cut -d ':' -f2 | cut -d ' ' -f3)
pID=$(lsusb | grep $trigger | cut -d ':' -f3 | cut -d ' ' -f1)
echo "vID=$vID"
echo "pID=$pID"
myfunc ($vID $pID);

send me an error:

./script3.bash: line 3: syntax error near unexpected token `cat'
./script3.bash: line 3: `cat > file.xml << EOFcat'

If I'm not mistaken, that means I can't use 'cat' inside function.

So, the question arises: is it possible to write a function that will allow you to create an XML file with the passed parameters inside?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
epoxit
  • 3
  • 1
  • `cat << EOFcat >file.xml` – David C. Rankin Nov 01 '18 at 14:07
  • 4
    Please take a look: http://www.shellcheck.net/ – Cyrus Nov 01 '18 at 14:07
  • See `function` section in [Obsolete and deprecated syntax](http://wiki.bash-hackers.org/scripting/obsolete). Also, take a look at this related post: [Passing parameters to a Bash function](https://stackoverflow.com/q/6212219/6862601). – codeforester Nov 01 '18 at 14:15
  • 2
    `{` is only special when it is a single word; otherwise, it's an ordinary character that can appear in a function name. You are attempting to define a function named `myfunc{`. To see, try `function myfunc{ { echo hi; }`. – chepner Nov 01 '18 at 14:15
  • Also, take a look at [How to cat <> a file containing code?](https://stackoverflow.com/q/22697688/6862601) regarding heredoc, `<<`. – codeforester Nov 01 '18 at 14:21
  • @DavidC.Rankin The order doesn't matter. – chepner Nov 01 '18 at 14:27

1 Answers1

0

even if available in bash, the function is not necessary and BTW is not standard

The declaration should probably be

myfunc () {

and call does not need them:

myfunc "$vID" "$pID"
chepner
  • 497,756
  • 71
  • 530
  • 681
OznOg
  • 4,440
  • 2
  • 26
  • 35