My job is installing multiple RaspPi's and they all need to be set up the same way, with a couple bash scripts, some installed programs and a cron that I wrote. Is there a way I can write one script to echo or printf the contents of the other bash scripts into the new Pi's?

Can I just do this?:

echo "$(cat myfile.sh)" > newbash.sh

Whilst you can use the line from your question it's quite inefficient as you're redirecting your script through two pipes. You can simplify it by just using (as @Wimm pointed out):

cat myfile.sh > newbash.sh

However, if you're installing the scripts I'd suggest you look into using the install command instead as this gives you finer control over the process by letting you set the permissions and UID/GID for the file and so on. Using cat will not copy any of metadata for your script; cp is a little better but you'd probably need extra commands like mkdir -p, chown and chmod to get thing just right. With install you could say something like:

install -Dp -v -o root -g staff -m 0755 myfile.sh /path/to/install/at/newbash.sh

This will...

  • create any missing directories along the destination path (/path/to/install/at)
  • make a copy of myfile.sh, called newbash.sh, at the destination
  • set the timestamp of newbash.sh to be the same as myfile.sh
  • change the owner of newbash.sh to root and the group staff
  • set the file permissions to 0755 (or -rwxr-xr-x if you prefer)
| improve this answer | |
  • I might've used the word "install" incorrectly in my explanation. Would cat or cp create the file under the current user's permissions? I would hate to use install and then not be able to run it without escalating considering these Pi's are designed to be mostly hands off. – T. Cubbin Feb 1 '19 at 20:29
  • Sure, use whatever user and group you like with install: -o pi -g pi for example. I didn't mean to imply that using cp is wrong only that there may be a better tool available with more control should you need it. I believe you can get the same outcome by using cp -p myfile.sh newbash.sh linux.die.net/man/1/cp – Roger Jones Feb 2 '19 at 0:03

You can use cp(copy) or cat:

cp myfile.sh newbash.sh
cat myfile.sh >newbash.sh
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.