1

right now I have multiple instructions that I have to add separately by manual labor (start terminal, copy paste instructions, insert instructions into text file), so I thought there must be a way to pack all of these commands into a single text file and have it run once.

For example I need to do 2 configurations:

1st Configuration

  1. create file with:

    sudo nano /usr/local/bin/loadFlysoftPOS
    
  2. copy following text into the above text file:

    #/bin/bash until pids=$(pidof mysqld)
    do  
    sleep 1
    done
    cd ~/Dokumente/dist/
    java -jar POSv2.jar > log.txt
    
  3. make it runnable by current user

    sudo chmod +x /usr/local/bin/loadFlysoftPOS
    

2nd Configuration

  1. create file:

    sudo nano /etc/udev/rules.d/10-local.rules
    
  2. insert text into file:

    SUBSYSTEMS=="usb", DRIVERS=="usb", ATTRS{idVendor}=="0525", ATTRS{idProduct}=="a700", SYMLINK+="usb/sewoo1", GROUP="lp"
    

So is it possible to store all those instructions into a single script and have it run once? Also importantly is to override any existing file, so if there already is a file called /etc/udev/rules.d/10-local.rules, its content should be cleared and overwritten by the contents provided by the new script.

Thanks guys for the enlightment!

Chiggiddi
  • 345

1 Answers1

2

nano is an interactive text editor - IMHO it isn't helpful for creating file content programatically.

For your first case, I'd suggest using a here document:

cat << \EOF > /usr/local/bin/loadFlysoftPOS
#!/bin/bash 

until $(pidof mysqld)
do  
  sleep 1
done
cd $HOME/Dokumente/dist/ && java -jar POSv2.jar > log.txt
EOF

chmod +x /usr/local/bin/loadFlysoftPOS

For the second case, a simple echo should do:

echo 'SUBSYSTEMS=="usb", DRIVERS=="usb", ATTRS{idVendor}=="0525", ATTRS{idProduct}=="a700", SYMLINK+="usb/sewoo1", GROUP="lp"' > /etc/udev/rules.d/10-local.rules

Put the whole thing in a file with its own shebang:

File: myscript.sh

#!/bin/sh

cat << \EOF > /usr/local/bin/loadFlysoftPOS
#!/bin/bash 

until $(pidof mysqld)
do  
  sleep 1
done
cd $HOME/Dokumente/dist/ && java -jar POSv2.jar > log.txt
EOF

chmod +x /usr/local/bin/loadFlysoftPOS

echo 'SUBSYSTEMS=="usb", DRIVERS=="usb", ATTRS{idVendor}=="0525", ATTRS{idProduct}=="a700", SYMLINK+="usb/sewoo1", GROUP="lp"' > /etc/udev/rules.d/10-local.rules

Make it executable, and run it with sudo

chmod +x myscript.sh
sudo ./myscript.sh
steeldriver
  • 136,215
  • 21
  • 243
  • 336
  • thank you so much for your effort in detailing how this work. Additionaly you provided the BashGuide/InputAndOutput that provide so much useful information in this matter. You just saved my day! Thanks again for your patentience and dilligence! – Chiggiddi Jan 16 '18 at 13:14
  • I did some further research and found this (https://askubuntu.com/questions/138908/how-to-execute-a-script-just-by-double-clicking-like-exe-files-in-windows) to help me in setting up the shell script as an executable similar to run by double-click a .exe file. Hope it helps someone. – Chiggiddi Jan 16 '18 at 13:20