rossoreed
Posts: 35
Joined: Mon Dec 30, 2013 9:48 am

Combining commands in bash script

Tue Dec 29, 2015 7:46 pm

I'm creating a rc.local script to create directories, empty files & permissions in a tmpfs volume, and found that I've got each command on a separate line.
Is there a way I can combine commands, similar to:
$ sudo mkdir /var/log/{redis,apache2,mysql}
(I tried that and it didn't work!)

sudo mkdir /var/log/redis
sudo mkdir /var/log/apache2
sudo mkdir /var/log/mysql
sudo touch /var/log/redis/redis-server.log
sudo touch /var/log/emoncms.log
sudo touch /var/log/mysql.log
sudo chmod 666 /var/log/redis/redis-server.log
sudo chmod 740 /var/log/apache2
sudo chmod 666 /var/log/emoncms.log
sudo chmod 666 /var/log/mysql/error.log
sudo chmod 666 /var/log/mysql.log
...and so on...

DirkS
Posts: 10363
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Combining commands in bash script

Tue Dec 29, 2015 8:31 pm

Something like

Code: Select all

for i in "redis" "apache2" "mysql" ; do mkdir /var/log/"$i"; done
:?:
Double quotes should only be necessary if there are spaces in the values

QuietZone
Posts: 89
Joined: Sat Dec 05, 2015 7:13 pm

Re: Combining commands in bash script

Tue Dec 29, 2015 10:17 pm

The underlying point to this is that rc.local (and similar) gets executed via "/bin/sh", which is actually bash masquerading as "old/standard" sh. The brace expansion trick only works in bash (and some other shells, having originally appeared as part of csh). When bash is running in "sh mode", it doesn't recognize brace expansion.

The solutions are (in order of preference):

1) Re-write things so as not to use brace expansion.

2) Arrange for your code to be run via "bash", not "sh".
"If you haven't got anything nice to say about anybody come sit next to me." — Alice Roosevelt Longworth

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Combining commands in bash script

Tue Dec 29, 2015 10:21 pm

Jessie runs it with this service file

Code: Select all

[Unit]
Description=/etc/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.local
After=network-online.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
SysVStartPriority=99
so that mean rc.local runs with whatever shell is defined in the shebang line. Change that and you can run rc.local with bash.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

rossoreed
Posts: 35
Joined: Mon Dec 30, 2013 9:48 am

Re: Combining commands in bash script

Tue Dec 29, 2015 11:52 pm

Well Dirk 'S answer seems to work OK, but it seemed to get very blurred thereafter!

In (very) basic terminology, is Dirk's reply the way to go?

rossoreed
Posts: 35
Joined: Mon Dec 30, 2013 9:48 am

Re: Combining commands in bash script

Wed Dec 30, 2015 11:07 pm

What if the command requires a value, such as 'chown mysql:adm' or requires sudo to execute?
I don't seem to get that working despite putting the command within double quotes ""sudo chown mysql:adm""

Paul

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Combining commands in bash script

Wed Dec 30, 2015 11:44 pm

A chown command isn't the sort of command you'd run at EVERY boot. Do it once and it's sticky.

If you insist on doing it in /etc/rc.local
/bin/chown mysql:adm /var/mysql/file.to.change.ownership
You do NOT need sudo, because rc.local is running with root privileges any way.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Combining commands in bash script

Thu Dec 31, 2015 5:35 am

DougieLawson wrote:A chown command isn't the sort of command you'd run at EVERY boot. Do it once and it's sticky.
However the OP is doing it to set up a tmpfs which will be empty after every boot.
rossoreed wrote:I'm creating a rc.local script to create directories, empty files & permissions in a tmpfs volume
In this particular case it does need to by run at every boot. I agree it's not the usual case though.
.

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Combining commands in bash script

Thu Dec 31, 2015 9:47 am

The mount options for his tmpfs must be wrong. Change those rather than mucking about with chown.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

rossoreed
Posts: 35
Joined: Mon Dec 30, 2013 9:48 am

Re: Combining commands in bash script

Thu Dec 31, 2015 10:05 am

Tmpfs appears to mount ok, but the issues are that MYSQL expects to find the /var/log/mysql folder structure and also it's mysql.log & error.log files. If they are not present, MySQL cannot create them and errors. Same applies with Redis.
I want to get the logs into tmpfs to extend the life of a system which runs on a SD card, and can be subject to power outages.
It does work ok if I simply list the commands, each to a line, but I'm trying to combine commands, similar to the way suggested above by DirkS, however I'm not sure of the format when combining chown or chmod commands, as they have a value too.

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Combining commands in bash script

Thu Dec 31, 2015 10:48 am

You can change those mysql directories in /etc/mysql/my.cnf
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

rossoreed
Posts: 35
Joined: Mon Dec 30, 2013 9:48 am

Re: Combining commands in bash script

Thu Dec 31, 2015 10:54 am

Yes I'm aware of that, but as I want the logs to be in tmpfs, I would still need to create empty log files with the correct permissions set for MySQL to write to, as it can't create them itself. Same with Redis.

Return to “General discussion”