M
M
Maxim Shadrin2014-04-15 09:23:40
bash
Maxim Shadrin, 2014-04-15 09:23:40

How to shorten a bash script?

Hello. With nix systems on "you", it was necessary to write a simple script on the freebsd server to copy some files to the user's home directory in order to slip this script into the task scheduler.
I wrote it as best I can, stupidly, like this:

#!/bin/bash
cp -f /etc/squid/URLs/accessed/access.sorted /home/security/urls/
cp -f /etc/squid/URLs/accessed/oper_kass_url /home/security/urls/
cp -f /etc/squid/URLs/denied/deny.url.uniq /home/security/urls/
cp -f /etc/squid/groups/full.dom /home/security/groups/
cp -f /etc/squid/groups/limit.dom /home/security/groups/
cp -f /etc/squid/groups/oper_kass.dom /home/security/groups/
cp -f /etc/squid/groups/unlim.dom /home/security/groups/
cp -f /etc/squid/squid.conf /home/security/

As I suppose, writing cp so many times is not good, is it possible to somehow shorten the entry, but so that it performs the same functionality, and most importantly, is there any point in this?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
Roman, 2014-04-15
@gen1s

I do not think that this makes sense (nothing will definitely become faster). But you can also write like this:

#!/bin/bash
cp -f /etc/squid/URLs/accessed/{access.sorted,oper_kass_url} /home/security/urls/
cp -f /etc/squid/URLs/denied/deny.url.uniq /home/security/urls/
cp -f /etc/squid/groups/{full.dom,limit.dom,oper_kass.dom,unlim.dom} /home/security/groups/
cp -f /etc/squid/squid.conf /home/security/

E
Elena Bolshakova, 2014-05-09
@liruoko

If the list of files stays the same forever - then everything is fine, only it would be better to change #!/bin/bash to #!/bin/sh
But if it is expected that the number or location of copied files will at least sometimes change -- I would do like this:

#!/bin/sh

for file in \
    /etc/squid/URLs/accessed/access.sorted \
    /etc/squid/URLs/accessed/oper_kass_url \
    /etc/squid/URLs/denied/deny.url.uniq \
    ;
do
    cp $file /home/security/urls/
done

for file in \
    /etc/squid/groups/full.dom \
    /etc/squid/groups/limit.dom \
    /etc/squid/groups/oper_kass.dom \
    /etc/squid/groups/unlim.dom \
    ;
do
    cp $file /home/security/groups/
done

for file in \
    /etc/squid/squid.conf \
    ;
do
    cp $file /home/security/urls/
done

It does not affect the speed of work, it turns out even more by lines than in the original version, and significantly more than in the gen1s proposal.
But the source files and target directories are separated on different lines, and this is easier to read and easier to edit. If you need to add copying of one or more files, just add lines of the same type. Throw a file out of copy -- delete a line. It's impossible to make a mistake ^_^
Just in case: there should be no spaces after backslashes (this is a way to break a long command into several lines).

S
Sergey, 2014-04-15
@bk0011m

Well, you can copy some files by mask .. But why? It won't be any faster.
In my opinion, it's normal. This is not 100500 lines of code, but only 9.
On the other hand, what is this copying for? Maybe just make symlinks?

I
iscsi, 2014-04-15
@iscsi

Why /bin/bash? Use /bin/sh wherever you can!
I support the option .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question