I am writing a bash shell script at the moment and one of the specifications is that I need to print an error message if the first argument, $1, is not -e or -d, this is what I have done:  if [ "$1" != "-e" ] || [ "$1" != "-d" ]; then         echo "Error: First argument must be either -e or -d"         exit 1 fi   this does not seem to work however, as even if i used -e or -d for the first argument, this error message is still printed. What am I doing wrong and how do I fix this?
79261
I tried to check if the PHONE_TYPE variable contains one of three valid values.  if [ "$PHONE_TYPE" != "NORTEL" ] || [ "$PHONE_TYPE" != "NEC" ] ||    [ "$PHONE_TYPE" != "CISCO" ] then     echo "Phone type must be nortel,cisco or nec"     exit fi   The above code did not work for me, so I tried this instead:  if [ "$PHONE_TYPE" == "NORTEL" ] || [ "$PHONE_TYPE" == "NEC" ] ||    [ "$PHONE_TYPE" == "CISCO" ] then     :        # do nothing else     echo "Phone type must be nortel,cisco or nec"     exit fi   Are there cleaner ways for this type of task?
21654
I'm writing a shell function that will be called from many different places and I want to ignore all errors occurring inside the function disabling the (possibly enabled) set -e shell option, executing set +e.  The problem is that I don't know if that option was set or not, so I don't know if it's really needed to disable it, and more important, if I need to set it again at the end.  So, how do I know if it is set or not? Is there any way to ignore errors of all commands inside a shell function ignoring the -e shell option if is set?
119249
I use -x for debug info while executing the shell script.  How can I redirect debug info only to a file?  Example script:  #!/bin/bash echo OK echo NO   i will get the below result when i run in debug mode  + echo OK OK + echo NO NO   I would like to redirect only the debug info to a file.  + echo OK + echo NO   And runtime output should print on the screen.  OK NO
206
From the Advanced Bash-Scripting Guide, to get the last command-line argument:     Or: lastarg=${!#}    This is an   indirect reference to the $# variable.    Note that lastarg=${!$#} doesn't work.
9236
How can I do something like this in bash?  if "`command` returns any error"; then     echo "Returned an error" else     echo "Proceed..." fi
65065
I am trying to create a event handler for icing. My Bash script looks like this right now:  #!/bin/bash # Event Handler for loggin out inactive RDP users  # $1 is $SERVICESTATE$ (OK WARNING UNKNOWN CRITICAL) # $2 is $SERVICESTATETYPE$ (SOFT HARD) # $3 is $SERVICEATTEMPT$ (1 through 4) # $4 is $SERVICDOWNTIME$ (0 no Downtime, &gt;0 Downtime active) # $5 is SRV29  # $6 is $host.name$ (1 through 4)   if  [ "$4" &gt; 0 ];  echo "in downtime, exit";  then exit fi  case "$1" in OK) echo "ok!" ;; WARNING) echo "warning!" ;; UNKNOWN) echo "unknown!" ;; CRITICAL) echo "critical!" ...   When I execute this script without my if statement at the top, everything works fine. But I want to check if $4 is greater than 0. This test condition always returns true and no matter what I enter inside this condition it always results in "in downtime, exit".  So even if [ "hello" = "hallo" ] it will go inside and exit right away. I also tried pretty much every variation with quotes, without, double brackets... and so on. I am obviously doing something wrong. Can anyone spot it?  Thanks in advance!
94297
The following bash syntax verifies if param isn't empty:   [[ !  -z  $param  ]]   For example:  param="" [[ !  -z  $param  ]] &amp;&amp; echo "I am not zero"   No output and its fine.  But when param is empty except for one (or more) space characters, then the case is different:  param=" " # one space [[ !  -z  $param  ]] &amp;&amp; echo "I am not zero"   "I am not zero" is output.  How can I change the test to consider variables that contain only space characters as empty?
96639
I defined the function f in Bash based on  (under "An option with an argument"):  f () {   while getopts ":a:" opt; do     case $opt in       a)         echo "-a was triggered, Parameter: $OPTARG" &gt;&amp;2         ;;       \?)         echo "Invalid option: -$OPTARG" &gt;&amp;2         return 1         ;;       :)         echo "Option -$OPTARG requires an argument." &gt;&amp;2         return 1         ;;     esac   done }   Whereas they use a script, I directly define the function in the shell.  When I first launch Bash and define the function, everything works: f -a 123 prints -a was triggered, Parameter: 123. But when I run the exact same line a second time, nothing is printed.  What's causing this behavior? It happens in Bash 3.2 and 4.3, but it works fine in Zsh 5.1. This is surprising because the example was supposed to be for Bash, not for Zsh.
17639
Take the following script:  #!/bin/sh sed 's/(127\.0\.1\.1)\s/\1/' [some file]   If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't need to escape the backslashes themselves (between the octets, or in the \s or \1). What's the rule here? What about when I need to use {...} or [...]? Is there a list of what I do and don't need to escape?
44865
With the following shell script, why I am getting errors   syntax error near unexpected token `else'   Shell Script   echo "please enter username" read user_name echo "please enter password" read -s pass echo ${ORACLE_SID} SID=${ORACLE_SID} if ["${ORACLE_SID}" != 'Test'] then sqlplus -s -l $USER_NAME/$PASS@$SID &lt;&lt;EOF copy from scott/tiger@orcl insert EMP using select * from EMP exit EOF else echo "Cannot copy" fi
20273
As a primarily Java programmer, I find the bash if-then construct quite confusing, especially regarding whitespace. Can anyone explain why the first one works, but not the second or third?  #works if [ -n $1 ]; then echo "parameterized"; fi  #output: ./links: Zeile 1: [-n: Kommando nicht gefunden. if [-n $1 ]; then echo "parameterized"; fi  #output: ./links: Zeile 1: [: Fehlende `]' if [ -n $1]; then echo "parameterized"; fi
113141
I'm working with a program that outputs error messages when something goes wrong, but does not set its exit status accordingly:  the exit status is always 0, indicating success.  I'd like to run this program from a shell script and get a non-zero exit status if it emits any error messages, so the script can tell that the program failed.  The error messages follow a predictable pattern that I can match with grep, and grep sets its exit status based on whether it found a match, so I can pipe the program's output into grep and negate the result with ! to get the exit status I want.  The problem is that if I pipe into grep, I can't see the program's output anymore, because grep consumes it.  I'd like to somehow scan the output for error messages but also display the output normally, since there are other important messages besides the errors.  Unfortunately, grep doesn't have an option to pass-through all of its input, matching and non-matching lines alike.  One approach I've found that mostly works is:  ! my_program | tee /dev/stderr | grep -q "error message"   This feeds the program's output into grep but also copies it to standard error, which isn't redirected, so I can see it.  That's OK when my script's standard output and standard error both go to a terminal (so it doesn't matter which one gets the messages), but it can cause problems if standard out and standard error are redirected to different places; the messages will end up in the wrong place.  Using bash or POSIX shell, and GNU tools, is there a (concise) way to scan a program's standard out and/or standard error streams with something like grep and set exit status accordingly, but also let both streams go to their normal destinations?  (Note that my_program writes its error messages to standard out, not standard error, so a solution that can only scan the standard output stream is OK, though not ideal.  And in case it makes a difference, I'm doing this on CentOS 7.)
58814
Both about -a and -e options in  is said:  -a file     True if file exists. -e file     True if file exists.    Trying to get what the difference is I ran the following script:  resin_dir=/Test/Resin_wheleph/Results  if [ -e ${resin_dir} ] ; then     echo "-e "; fi  if [ ! -e ${resin_dir} ] ; then     echo "! -e"; fi  if [ -a ${resin_dir} ] ; then     echo "-a"; fi  if [ ! -a ${resin_dir} ] ; then     echo "! -a"; fi   /Test/Resin_wheleph/Results exists and is a directory. And this is what I get:  -e -a ! -a   which seems to be a little strange (notice -a and  ! -a). But when I use double brackets (e. g. if [[ -e ${resin_dir} ]]) in the similar script it gives reasonable output:  -e -a   So:    What is a difference between -a and -e options? Why -a produces a strange result when used inside single brackets?
23870
I've used the following script to see if a file exists:  #!/bin/bash  FILE=$1      if [ -f $FILE ]; then    echo "File $FILE exists." else    echo "File $FILE does not exist." fi   What's the correct syntax to use if I only want to check if the file does not exist?  #!/bin/bash  FILE=$1      if [ $FILE does not exist ]; then    echo "File $FILE does not exist." fi
12979
I would like to write a bash script with unknown amount of arguments.  How can I walk through these arguments and do something with them?  A wrong attempt would look like this:  #!/bin/bash for i in $args; do      echo $i done
91891
I was looking for a command to limit numbers read in from stdin.  I wrote a little script for that purpose (critique is welcome), but I was wondering if there was not a standard command for this, simple and (I think) common use case.  My script which finds the minimum of two numbers:  #!/bin/bash # $1 limit  [ -z "$1" ] &amp;&amp; { echo "Needs a limit as first argument." &gt;&amp;2; exit 1; }  read number  if [ "$number" -gt "$1" ]; then         echo "$1" else         echo "$number" fi
48283
I am taking the Linux Essentials class and was doing well until I hit the scripting chapter. I simply do not understand these concepts.  Wondering if someone can break the following down into ultra simplistic terms or point me to a better reference to learn it. I'm currently using netacad's curriculum.        From the text book (with minor formatting changes):     There are some special variables in addition to the ones you set. You   can pass arguments to your script:  #!/bin/bash echo "Hello $1"       A dollar sign followed by a number N corresponds to the Nth argument   passed to the script. If you call the example above with ./test.sh the   output will be Hello Linux. The $0 variable contains the name of the   script itself.      After a program runs, be it a binary or a script, it returns an exit   code which is an integer between 0 and 255. You can test this through   the $? variable to see if the previous command completed successfully.     I understand how to assign variables and how they work with the $ but the whole issue with $0 and $1 -- I just don't get it.    Any help would be much appreciated.