I'm trying to workout why this doesn't work
$result=`[ pgrep -x "gedit" ] && echo "Running" || echo "Stopped" `
I get:
-bash: [: -x: binary operator expected
If I run pgrep -x "gedit" direct I get process ID's
Can someone advise why this fails ?
Thanks
You don't need the brackets to run and check the exit status of pgrep:
result=$(pgrep -x "gedit" > /dev/null && echo "Running" || echo "Stopped")
You can use the following code to solve your problem:
#!/bin/bash
result=\`pgrep -x gedit && echo running || echo not\`
echo $result
Related
This simple script - to check whether a process is running and then send an email if it's not - works fine:
#!/bin/bash
if pgrep -f "ghost.py" > /dev/null
then
echo "Running" > /dev/null
else
echo "Looks like ghost.py stopped" | mail -s "ghost.py down" shaney.jones#gmail.com
fi
However, my attempt to have an OR operator in there to check two scripts fails:
#!/bin/bash
if [ pgrep -f "ghost.py" ] || [ pgrep -f "ghost2.py" ] > /dev/null
then
echo "Running" > /dev/null
else
echo "Looks like a script stopped" | mail -s "Script down" shaney.jones#gmail.com
fi
The error just says '-bash: [: -f: binary operator expected'
Drop the brackets as it's completely another command (usually equivalent to test):
if pgrep -f "ghost.py" || pgrep -f "ghost2.py" > /dev/null
then
# Your stuff
See help test and man test for more details about the [ command.
Hello I am new to shell script and I need to handle the error coming from a command being excuted inside echo like the following
echo -e "some internal command that I can't share \nq" | nc localhost 10000
I want to say
if [[ there's no error ]]
try
echo "YOUR SUPERSECRET COMMAND" | nc localhost 10000 | grep "Your expected error"
if [[ $? -eq 0 ]]; then
echo "Do something useful with error"
else
echo "Success"
fi
grep return 0 on matching and returns 1 when it doesn't find matching string.
The shell variable $? will give you the exit code. So, you could do:
echo -e "some internal command that I can't share \nq" | nc localhost 10000
rc=$?
if [[ $rc == 0 ]]; then
echo "success"
fi
Or simply,
if echo -e "some internal command that I can't share \nq" | nc localhost 10000; then
echo "success"
fi
Here is a concise way of doing it:
internalcommand &>/dev/null 2>&1 && echo OK || echo FAILED
If internalcommand succeeds OK will be printed to stdout, if it fails FAILED is printed.
Note tested on Bash v4
I am working on a shell script where I have requirement that I need to run all my command inside from a function.
I have written below code in my script.
#!/bin/bash
function RUN_CMD()
{
CMD=${*}
echo "Command launched: \"${CMD}\"."
($*)
RETVAL=$?
if [ $RETVAL = 0 ] ; then
echo "Command successful: \"${CMD}\"."
else
echo "Command failed with RC=${RETVAL}: \"${CMD}\"."
fi
return $RETVAL
}
_cmd="tr -d 'the' < file.txt > file.new"
RUN_CMD "$_cmd"
When executing this code I am getting below error.
tr: extra operand ‘<’
Try 'tr --help' for more information.
Command failed with RC=1: "tr -d 'the' < file.txt > file.new".
I don't what I am doing wrong. Please help.
you could run your command in a subshell:
#!/bin/bash
function RUN_CMD()
{
OUT=$(/bin/bash -c "$1")
RETVAL=$?
echo "Command launched: \"$1\"."
if [ $RETVAL -eq 0 ] ; then
echo "Command successful: \"$1\", \"OUT=${OUT}\""
else
echo "Command failed with RC=${RETVAL}: \"$1\", \"OUT=${OUT}\""
fi
return $RETVAL
}
_cmd="tr -d 't' < file.txt > file.out"
RUN_CMD "$_cmd"
Best,
Julian
You can't handle redirections (pipes, etc.) when you run $* or $# in a function this way. The redirection operators < and > get passed to tr as arguments. You need to use redirection when calling the function:
_cmd="tr -d the"
RUN_CMD "$_cmd" < file.txt > file.new
Or use eval:
_cmd="tr -d 'the' < file.txt > file.new"
eval $_cmd
I have written a shell script to reconnect my router.
When I run it on my PC with Cygwin, it works fine, but now I want to run it on my smartphone in a SL4A shell, and I get an error:
reconnect.sh[4] syntax error: '{' unmatched
This is the script I'm using:
#! /system/bin/sh
echo "script start"
sleep 3
strindex() {
x="${1%%$2*}"
[[ $x = $1 ]] && echo -1 || echo ${#x}
}
Why am I getting this error?
I have found the error:
#!/bin/sh
strindex() { x="${1%%$2*}" [[ $x = $1 ]] && echo -1 || echo ${#x} }
in one line.
Cygwin seems to resolve linebreaks but SL4A doesn't!
I am trying to see if I found something using grep or not with this
found=`grep -F "something" somefile.txt`
if ((${#found} == 0)); then
echo "Not Found"
else
echo "Found"
fi
I succeeded using above logic that if grep found something it stores the output in found variable but the issue I am facing is with if condition. Whenever found=0 it gives me some error like that
final.sh: 13: final.sh: 0: not found
FYI: final.sh is the script name
The problem is that you're writing bash specific code, but running it with sh. In bash, (( .. )) is an arithmetic context, while in POSIX sh, it's merely two nested subshells, causing it to try to execute the number as a command.
You can run it with bash instead of sh by specifying #!/bin/bash in the shebang, and/or using bash yourfile instead of sh yourfile if you invoke it that way.
The correct way for your example, however, is to use grep's exit status directly:
if grep -q something somefile
then
echo "found"
else
echo "not found"
fi
To check whether some string is in your file, you can use the return status from grep
grep -q something somefile.txt
if [ $? -eq 0 ]
then
echo "found"
else
echo "not found"
fi
a shorter form will be
grep -q something somefile.txt && echo found || echo not found
found=$(grep -F "something" somefile.txt)
if [ $? = 0 ]; then # $? is the return status of a previous command. Grep will return 0 if it found something, and 1 if nothing was found.
echo "Something was found. Found=$found"
else
echo 'Nothing was found'
fi
I find this code more elegant than other answers.
But anyway, why are you writing in sh? Why don't you use bash? Are you sure that you need that portability? Check out this link to see if you really need sh
Here's how I do that sort of thing:
found=$(grep -F "something" somefile.txt)
if [[ -z $found ]]; then
echo "Not found"
else
echo "Found"
fi