0

I am trying to create a Bash script that will run on system startup and modify the permissions for a number of ttyA ports. I'm unsure how to go about this and was wondering if I could get some help.

My current code is:

#!/bin/bash
TTYA=$ ls /dev/ttyA11

echo $TTYA

echo 'outside' if [[ $$TTYA == 'ttyA11' ]] then echo 'inside' chmod g+rw /dev/ttyA* fi

Output:

$ sudo ./rc.serial
/dev/ttyA11

outside

Unfortunately this method does not work due to the if statement not being true. I'm unsure if I've correctly told my variable to check for ttyA11.

  • Why not add your users to plugdev group instead? – Arkadiusz Drabczyk Oct 12 '20 at 10:56
  • I just wanted to know if it's possible to create a bash script that will execute my command on a number of ports. I'm not aware of plugdev, but I do know my ttyA ports need to be assigned to the dialout group. – user427317 Oct 12 '20 at 11:04
  • Sorry, I meant dialout group of course. – Arkadiusz Drabczyk Oct 12 '20 at 11:04
  • That's ok. I just know that I need to be able to replicate these two commands: chmod g+rw /dev/ttyA* and chgrp dialout /dev/ttyA* in a bash script that I will run during system startup. I'm certain there's an efficient way to do this that my current listed method. But I'm new to Bash scripting and only have a very tiny amount of knowledge on this – user427317 Oct 12 '20 at 11:07
  • I'm having a hard time trying to understand what you want to do. Do you want to check if /dev/ttyA11 exists? – Arkadiusz Drabczyk Oct 12 '20 at 11:09
  • Sorry for the confusion. The ttyA11 port is loaded by a driver every time the system loads however it has the wrong permissions and group set. So to combat this I would like to make a script file that updates the permissions and group accordingly when the system starts up. I don't want to keep updating the permissions manually every time I restart my machine basically. Hope this helps? – user427317 Oct 12 '20 at 11:13
  • On Linux, one would normally use a udev rule to set the group aan mode when the device is loaded - see for example Allow non-root user to read/write /dev files – steeldriver Oct 12 '20 at 13:08
  • @steeldriver I had seen something about udev in a forum post. Thank you for providing a link and through reading the solution listed here I was able to successfully make a udev rule. The file is just rather large now with over 40 lines of individual port rules haha. – user427317 Oct 12 '20 at 15:27

2 Answers2

1

There are a number of issues with your script that need attention.

TTYA=$ ls /dev/ttyA11

Not sure what you are trying to accomplish here, but if you do TTYA=$(ls /dev/ttyA11) (note the correct syntax for assigning the output of a command to a variable), TTYA wil contain /dev/ttyA11, if that device exists.

echo $TTYA

You should probably put quotes here, as in echo "$TTYA"

echo 'outside'
if [[ $$TTYA == 'ttyA11' ]]

$$ will expand to the PID of the running shell. So, the if-statement will always be false. If you use $TTYA (single dollar), the statement will also be false, because TTYA has a /dev/ in front of the device name.

then
  echo 'inside' 
  chmod g+rw /dev/ttyA*
fi

The logic of the script is strange; it looks like you want to make /dev/ttyA* group-rw if /dev/ttyA11 exists. But in that case, would

if [ -e  /dev/ttyA11 ] ; then
   chmod g+rw /dev/ttyA*
fi

be more logic?

Ljm Dullaart
  • 4,643
  • Thank you for providing me a detailed insight into the matter. Honestly I wasn't sure what I was attempting there, but after reading your solution it makes much more sense to me. – user427317 Oct 12 '20 at 15:25
0

Try this:

TTYA='ls /dev/ttyA11'

eval $TTYA

echo $TTYA

echo "outside"

if [ $TTYA == 'ttyA11' ]

then

echo "inside"

chmod g+rw /dev/ttyA*

fi

jsbillings
  • 24,406
  • 1
    This will produce bash: [: too many arguments, because TTYA contains ls /dev/ttyA11 (so, with a space in it). Shellcheck will tell you that you must quote $TTYA, but even then, the if condition will always be false. – Ljm Dullaart Oct 12 '20 at 16:08