Why does autostart script on Linux not work - linux

I got an auto start bash script, to start fluidsyth and aconnect command automatically after rebooting my linux system (RPI-3):
#!/bin/bash
(STOP=$((SECONDS+5))
until [[ $SECONDS -ge $STOP || $(ps -C fluidsynth -o stat=) =~ S ]]; do:; done &&
aconnect 20:0 128:0 &)
fluidsynth -a alsa -g 5 /usr/share/sounds/sf2/FluidR3_GM.sf2
When I run this script, it popped up with the following error:
./piano4.sh: line 4: syntax error near unexpected token `done'
./piano4.sh: line 4: `until [[ $SECONDS -ge $STOP || $(ps -C fluidsynth -o stat=) =~ S ]]; do:; done &&'
Till now I cannot solve this error. There are no ^M in the script (I already checked).
Can anyone help me to get it up and running?
Thanks
There are no ^M in the script (I already checked)

I found the answer by changing the script into:
#!/bin/bash (STOP=$((SECONDS+15)) until [[ $SECONDS -ge $STOP || $(ps -C fluidsynth -o stat=) =~ S ]]; do echo "" > /dev/null; done && aconnect 20:0 128:0 &) fluidsynth -a alsa -g 5 /usr/share/sounds/sf2/FluidR3_GM.sf2
Thanks

Related

How to read command output and use it silently on a bash script?

I'm new on bash. I want to write a script which will run iwconfig command the output should show me my wlan* mode is Managed or Monitor?
I had tried this like following but it is not working and runs the command output. I just need to echo which mode is it.
IWCONFIG_LOG="$(iwconfig)"
if [[ "$IWCONFIG_LOG" == *Mode:Managed* ]]; then
echo "Managed"
elif [[ "$IWCONFIG_LOG" == *Mode:Monitor* ]]; then
echo "Monitor Mode"
fi
Looks like you want to use Bash (not sh) in order to get this accomplish.
So, try this:
#!/bin/bash
IWCONFIG_LOG="$((iwconfig | grep Mode) 2> /dev/null)"
if [[ $IWCONFIG_LOG == *"Mode:Managed"* ]]; then
echo "Managed"
elif [[ $IWCONFIG_LOG == *"Mode:Monitor"* ]]; then
echo "Monitor Mode"
fi
Save it as a file and chmod 755 it to make it executable or execute it using "bash" instead "sh".
There are several modes, not only Managed or Monitor, so seems you only want to detect one of these two.
To see all available modes read man iwconfig
Since iwconfig writes also to standard error:
if [[ $(iwconfig 2>/dev/null | grep "Managed") ]]; then
echo "Managed"
else
echo "Monitor"
fi
or, as pointed out by #choroba in comments:
if iwconfig 2>/dev/null | grep -q Managed ; then
echo "Managed"
else
echo "Monitor"
fi

How to see JBOSS has started/stopped from commandline

Is there some linux/jboss command I can use in a script to see if jboss started.
I have to start up a couple in a specified order and one of the jbosses must
be started before the others can be started up.
/T
Refer this link ,With the following command you can try to read the server "Started" attribute
twiddle get "jboss.system:type=Server" Started
Started=true
On Fedora 19, you can install the jboss-as package, which comes with a nice startup script, and you can check the status just like any other daemon: systemctl status jboss-as
Similarly for Fedora 20 and Wildfly: systemctl status wildfly .
Here is a script I used:
#!/bin/bash
CHECK_TIMEOUT=$1;
if [[ $1 =~ ^[0-9]+$ ]]; then
echo "Checking if JBoss is running with timeout of $1 s.";
else
echo "Checking if JBoss is running with default timeout of 60 s.";
CHECK_TIMEOUT=60;
fi
while [[ $CHECK_TIMEOUT -ne 0 ]]
do
sleep 1;
JBOSS_STATE=`~/jboss/bin/jboss-cli.sh 'connect,:read-attribute(name=server-state),q' | grep result`;
if [[ -z $JBOSS_STATE ]]; then
JBOSS_STATE="stopped";
else
JBOSS_STATE=`echo "$JBOSS_STATE" | tr -s \" " " | cut -d ' ' -f 4`;
fi
echo "JBoss is $JBOSS_STATE";
if [[ $JBOSS_STATE == "running" ]]; then
exit 0;
fi
((CHECK_TIMEOUT-=1));
done
exit 1;

Bash script: Syntax error in conditional expression

I'm new around the neighborhood and stuck with a syntax error. Please take a look and maybe someone can assist. I'm trying to run the following script:
#!/bin/bash
main () {
dpkg -query -s $1 &> /tmp/pkg_verify
if grep -q 'not installed' /tmp/verify
then
echo -e "\e[31m$1 is not installed. installing..\e[0m"
apt-get install $1
echo -e "\e[31m$1 is not installed and ready to use\e[0m"
else
echo -e "\e[31m$1 is already installed\e[0m"
fi
rm -f /tmp/pkg_verify
for test in $#; do main $test; shift; done
echo -e "\e[31mDone\e[0m"
}
for test in $#; do main $test; shift; done
echo -e "\e[31mDone\e[0m"
But when I try to execute it I'm facing with endless loop:
grep: /tmp/verify: No such file or directory
16 is already installed
I truly tried to find the answer, tried to change the if to couple of different forms but with out any success. Does any one have an idea why that is? What should I change so that the script can run?
Thanks in advance to all the helpers.
You have two else following each other. That can't work. It's either elif condition or just a single else.
The infinite loop is caused by main calling itself recursively.
And third, it's probably a bug to shift when iterating with for i in "$#".
To debug a script (free of syntax errors) use set -x near the beginning.
Replace this line:
if [[ -z `grep 'not installed' /tmp/pkg_verify` ]]
with this if condition:
if grep -q 'not installed' /tmp/pkg_verify
Full Script:
main () {
dpkg-query -s "$1" > /tmp/pkg_verify
if grep -q 'not installed' /tmp/pkg_verify
then
echo -e "\e[31m$1 is not installed. installing..\e[0m"
apt-get install "$1"
echo -e "\e[31m$1 is not installed and ready to use\e[0m"
else
echo -e "\e[31m$1 is already installed\e[0m"
fi
}
rm -f /tmp/pkg_verify
for test in $#; do main "$test"; done
echo -e "\e[31mDone\e[0m"

Any alternatives or ways to make this script faster?

I have this script which displays the terminal type being used. So for instance if you were running konsole it would display konsole. The script needs to go into another program that runs when a terminal is opened so it has to be very fast. Here's what I have so far
#!/bin/bash
shopt -s extglob
SHELLTTY=$(exec ps -p "$$" -o tty=)
P=$$
while read P < <(exec ps -p "$P" -o ppid=) && [[ $P == +([[:digit:]]) ]]; do
if read T < <(exec ps -p "$P" -o tty=) && [[ $T != "$SHELLTTY" ]]; then
ps -p "$P" -o comm=
break
fi
done
When the script is saved into a file it takes this long for it to run.
[~]$ time ./termgrab
konsole
real 0m0.063s
user 0m0.017s
sys 0m0.040s
The whole program itself takes .04 seconds so this slows it down considerably. Does anyone have any suggestions to make the script any faster or any alternative ways to achieve the same thing?

while in bash script

I am making a bash script. the objective is:
execute a program wait some seconds reset the program and repeat the process.
I make 2 scripts but i don't know where is the error...
#!/bin/bash
while true;
do
seg=`date +%M`;
if [[ "$seg" -eq "30" ]];
then killall sox;
echo "reset";
fi
done
bash: error sintáctico cerca del elemento inesperado `;'
#!/bin/bash
while true;
do
nice -n-10 sox -q -V0 --multi-threaded -t alsa hw:2,0 -t alsa pcm.default &&
done
bash: error sintáctico cerca del elemento inesperado `done'
Issues with Script #1:
The ; notation is to run multiple commands on the same line, one after another. Bash syntax requires that while and do on separate lines (same with if ... and then, and separated by ; if on the same line. Command statements are not normally terminated with a ; char in bash.
Change your code from:
#!/bin/bash
while true;
do
seg=`date +%M`;
if [[ "$seg" -eq "30" ]];
then killall sox;
echo "reset";
fi
done
To:
#!/bin/bash
while true
do
seg=`date +%M`
if [[ "$seg" -eq "30" ]]; then
killall sox
echo "reset"
fi
done
Issues with Script #2:
& means to run the command as a background process. && is used for conditional command chaining, as in: "If the previous command before && succeeds, then run the next command after the &&"
Change from:
#!/bin/bash
while true;
do
nice -n-10 sox -q -V0 --multi-threaded -t alsa hw:2,0 -t alsa pcm.default &&
done
To:
#!/bin/bash
while true
do
nice -n-10 sox -q -V0 --multi-threaded -t alsa hw:2,0 -t alsa pcm.default &
done

Resources