Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
What is wrong with my below script?
I am new to scripting and trying out simple scripts. I am getting an error with my script.
START_OUT = `grep -c "Start Report" jeevagan/test_scripts/log.txt`
FINISH_OUT = `grep -c "Finished Report" jeevagan/test_scripts/log.txt`
if [$START_OUT == $FINISH_OUT]
then
echo "All good"
else
echo "Warning!!!Monitor Logs"
fi
bash/sh is very space sensitive. You want
if [ $START_OUT == $FINISH_OUT ]
Note the spacing around the brackets. "=" may be used instead of "==" for strict POSIX compliance. See here for more details and note the comment
In a script, the different parts of the if statement are usually
well-separated.
try leaving spaces on the if statement and add quotes on the variables, like this
if [ "$START_OUT" == "$FINISH_OUT" ]
If this is still not working try this exactly, this works for sure, if it still gives you an error, then use echo brefore the if statement and check what $START_OUT and $FINISH_OUT variables have stored, because the problem could be there, after you do this give us feedback:
if test "$START_OUT" = "$FINISH_OUT"
okayyyy, this was one of your problems, the other problem is that you left a space when you used grep and tried to add it in the variable...
i will rewrite your code, copy and paste it please. as you had it, it was like you calling the START_OUT command which obviously doesnt exist.... and tell me if it worked.
START_OUT=`grep -c "Start Report" jeevagan/test_scripts/log.txt`
FINISH_OUT=`grep -c "Finished Report" jeevagan/test_scripts/log.txt`
if test "$START_OUT" = "$FINISH_OUT"
then
echo "All good"
else
echo "Warning!!!Monitor Logs"
fi
if [$START_OUT == $FINISH_OUT] is wrong. take care about spaces. it should be like
if [ $START_OUT == $FINISH_OUT ]
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
I am following https://lifehacker.com/add-a-handy-separator-between-commands-in-your-terminal-5840450 to create a nice separator between commands in the terminal in Linux. Specifically, CentOS 8.
I am trying to modify the script to output the username of the user who ran the command.
Here is what I came up with.
# Fill with minuses
# (this is recalculated every time the prompt is shown in function prompt_command):
fill="--- "
reset_style='\[\033[00m\]'
if [ -z "$VIM" ];
then status_style=$reset_style'\[\033[0;90m\]' # gray color; use 0;37m for lighter color
else status_style=$reset_style'\[\033[0;90;107m\]'
fi
prompt_style=$reset_style
command_style=$reset_style'\[\033[1;29m\]' # bold black
# Prompt variable:
OLD_PS1="$PS1"
PS1="$status_style"'$fill $USER \t\n'"$prompt_style$OLD_PS1$command_style"
# Reset color for command output
# (this one is invoked every time before a command is executed):
trap 'echo -ne "\e[0m"' DEBUG
function prompt_command {
# create a $fill of all screen width minus the time string and a space and USER and a space:
let fillsize=${COLUMNS}-10-${#USER}
fill=""
while [ "$fillsize" -gt "0" ]
do
fill="-${fill}" # fill with underscores to work on
let fillsize=${fillsize}-1
done
# If this is an xterm set the title to user#host:dir
case "$TERM" in
xterm*|rxvt*)
bname=`basename "${PWD/$HOME/~}"`
echo -ne "\033]0;${bname}: ${USER}#${HOSTNAME}: ${PWD/$HOME/~}\007"
;;
*)
;;
esac
}
PROMPT_COMMAND=prompt_command
Line 15 added " " and $USER to what is generated.
Line 25 changed to include an extra space and the length of the variable $USER
It looks just like I want it to.
Terminal Screenshot
But, I would like to update the code to output if I ran a command as sudo or not.
Ideally, it would change the name to root or whatever the root user name is.
I have tried several things, mainly I tried using whoami but this always returns my username not root.
If I run sudo whoami I get root but not from the script.
I also tried EUID No dice.
At this point, I have left the code in working condition with the $USER reference but I am willing to change it to whatever it needs to be.
Solution provided by u/pLumo
Solution Limitations:
There are cases not covered, for example sudo --user=some_user .... I think it's fairly easy to further enhance the awk script.
As it relies on the history, it won't work with commands you do not have in history, e.g. when using HISTCONTROL=ignoreboth and issue a command with a space in front.
# Fill with minuses
# (this is recalculated every time the prompt is shown in function prompt_command):
fill="--- "
reset_style='\[\033[00m\]'
if [ -z "$VIM" ];
then status_style=$reset_style'\[\033[0;90m\]' # gray color; use 0;37m for lighter color
else status_style=$reset_style'\[\033[0;90;107m\]'
fi
prompt_style=$reset_style
command_style=$reset_style'\[\033[1;29m\]' # bold black
# Prompt variable:
OLD_PS1="$PS1"
PS1="$status_style"'$fill $name \t\n'"$prompt_style$OLD_PS1$command_style"
# Reset color for command output
# (this one is invoked every time before a command is executed):
trap 'echo -ne "\e[0m"' DEBUG
function prompt_command {
# create a $fill of all screen width minus the time string and a space and USER and a space:
name=$(fc -l -1 | awk -v u="$USER" '{if ($2=="sudo") { if ($3=="-u") u=$4; else u="root"; }; printf "%s",u}')
let fillsize=${COLUMNS}-10-${#name}
fill=""
while [ "$fillsize" -gt "0" ]
do
fill="-${fill}" # fill with underscores to work on
let fillsize=${fillsize}-1
done
# If this is an xterm set the title to user#host:dir
case "$TERM" in
xterm*|rxvt*)
bname=`basename "${PWD/$HOME/~}"`
echo -ne "\033]0;${bname}: ${USER}#${HOSTNAME}: ${PWD/$HOME/~}\007"
;;
*)
;;
esac
}
PROMPT_COMMAND=prompt_command
Terminal output:
Not sure if I should delete this question or not. Or if there is a better way to adjust this. Open to suggestions.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am writing a script to compile a program in bash and my if/else statement on line 84 is not working. I get this error:
./build2.sh: 84: [: no: unexpected operator
What is the problem?
http://www.pasteall.org/62904/bash
Thanks in advance!
"Is not working" is very faint. However, I'll give it a try...
If enter is pressed without typing any reply, $putProgramOn is empty, and
if [ $putProgramOn == "no" ] resolves to if [ == "no" ], which is a syntax error, so you need at least to quote the reply in the conditional:
if [ "$putProgramOn" == "no" ]
See also Bash Comparison Operators.
BTW: With esac you can simply have shortcuts "y" and "n" evaluated as well:
read -p 'Would you like to put the program on the flash drive? Awnser yes or no: ' putProgramOn
case "$putProgramOn" in
n|no)
echo "putProgramOn=no"
;;
y|yes)
echo "putProgramOn=yes"
;;
*)
echo "putProgramOn=\"putProgramOn\""
exit 1
;;
esac
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am new to Linux and Shell scripting, so please help me with some patience.
Could you please let me know how to fix this problem?
Below is the error
./test.sh: line 5: syntax error near unexpected token `else'
./test.sh: line 5: `else'
Below is the simple if statment in shell scripting.
I use vi editor
#!/bin/bash -x
age=10;
if(age -lt 13)
echo "$age"
else
echo "xx"
Any information is highly appreciable.
Matt
Don't end a statement with ;. This is not c/c++/java program.
Conditions in script are enclosed with [] not ()
Please read how to use if statement/syntax. Do google and then try.
Syntax of if:
if [ conditional expression ]
then
#statements
...
fi
So your program is:
#!/bin/bash
age=10
if [ $age -lt 13 ]
then
echo "something"
else
echo "some other thing"
fi
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am attempting a simple file existence check in my bash script:
FILE_TO_CHECK=/home/username/path/to/file
if [ ! -f $FILE_TO_CHECK]; #line 9 in actual script
then
echo File not found.
fi
Seems simple enough to me, but I'm getting the following error and I don't know why:
/path/to/script: line 9: [: missing `]'
I'm not exactly a bash expert, but I was pretty sure a backtick is not necessary in this context. What gives?
Missing space before the closing ].
You have to understand that [ is a command and everything following it, until the ;, are its arguments. Command [ expects its last argument to be ]. But if you omit the space, then ] becomes the last character of the previous argument.
It might seem that [ ] is part of the if syntax. It's not the case. if has to be followed by any command, and if evaluates its exit status.
if true; then foo; fi
In the above line true is a command too.
$ which true
/bin/true
true is a command with the sole purpose of always having a true (0) exit status.
You could also try:
if (test ! -f $FILE_TO_CHECK);
or
if !(test -f $FILE_TO_CHECK);
as [ is a shorthand for the test command.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
How can I put the current running process name into a GNOME Terminal tab title (or title bar when there's only one tab)?
Although https://superuser.com/questions/42362/gnome-terminal-process-name-in-tab-title provides a solution (below), it completely clutters each tab with garbage when it starts so as to appear broken. Is there a better way?
case "$TERM" in
xterm*|rxvt*)
set -o functrace
trap 'echo -ne "\e]0;$BASH_COMMAND\007"' DEBUG
PS1="\e]0;\s\007$PS1"
;;
*)
;;
esac
Well, since everyone already seems to know David Pashley's solution I'm kind of surprised it took me so long to find this one.
It actually takes care of the bash-completion problem.
To be clear: I did nothing on my own here but research. All credit goes to Marius Gedminas (http://mg.pov.lt/blog/bash-prompt.html).
This works perfectly for me with Gnome-Terminal/Terminator
# If this is an xterm set the title to user#host:dir
case "$TERM" in
xterm*|rxvt*)
PROMPT_COMMAND='echo -ne "\033]0;${USER}#${HOSTNAME}: ${PWD}\007"'
# Show the currently running command in the terminal title:
# http://www.davidpashley.com/articles/xterm-titles-with-bash.html
show_command_in_title_bar()
{
case "$BASH_COMMAND" in
*\033]0*)
# The command is trying to set the title bar as well;
# this is most likely the execution of $PROMPT_COMMAND.
# In any case nested escapes confuse the terminal, so don't
# output them.
;;
*)
echo -ne "\033]0;${USER}#${HOSTNAME}: ${BASH_COMMAND}\007"
;;
esac
}
trap show_command_in_title_bar DEBUG
;;
*)
;;
esac