0

I have this script on about 20 servers.

 CASE=$1

 case $CASE in
"multipath")
    status=$(service multipathd status | awk '{gsub(/[.]/,"");print $NF}')
        if [ $status = 'running' ]; then
        echo 0
        else
        echo 1
        fi
  ;;
"hbaport")
        output=0
    port_array=($(cat /sys/class/fc_host/*/port_state))
        for port in ${port_array[@]}
    do
            if [ $port != 'Online' ]; then
        output=1
            fi
        done
            echo $output
  ;;
"netface")
  inter=0
      ifconfig=$(ifconfig)
      interface_array=($(ls -l /etc/sysconfig/network-scripts/ifcfg-* | awk -F '-' '{print $NF}' | grep -iv 'lo'))
      for interface in ${interface_array[@]}
  do
  if [[ $ifconfig == *"$interface"* ]]; then
  operstate=$(cat /sys/class/net/$interface/operstate)
           if [ $operstate != 'up' ]; then
               inter=1
               fi
  fi
      done
      echo $inter
  ;;
 esac

when i run this command:
/opt/zabbix_agent/share/scripts/OSscript "netface"
it returns:
0
i just added this script to a new server and when i run above command i get this:

: command not foundhare/scripts/OSscript: line 2:
'opt/zabbix_agent/share/scripts/OSscript: line 3: syntax error near 
 unexpected token `in
'opt/zabbix_agent/share/scripts/OSscript: line 3: case $CASE in  

why i get this error? OS is CentOS 6.9

  • Did you miss something during copy/paste? Use shellcheck.net to analyse your script. – Thomas Dec 30 '18 at 11:10
  • 4
    Looks like line endings are mucked up. Make sure there are no CR characters at the end of each line, – icarus Dec 30 '18 at 11:13
  • That case $CASE should be case "$CASE" – Rui F Ribeiro Dec 30 '18 at 11:16
  • i checked with shellcheck. no serious error. CR? . "$CASE" didnt work. – BlackCrystal Dec 30 '18 at 12:11
  • 2
    CR is control-M (carriage return). The first line of the error output ": command not foundhare/scripts/OSscript: line 2:" looks like it is partially overwritten. use cat -v for example to check – icarus Dec 30 '18 at 12:26
0

well according to icarus comment I used cat -v and saw that ^M are considered as a part of command. I used these solutions at this post to remove them:
https://stackoverflow.com/questions/800030/remove-carriage-return-in-unix
they did work on a test script not on OSscript. finally i found out that my files are corrupted. I removed OSscript and create it again and added my script. it worked.

  • If you say @icarus rather than icarus I will be notified of the answer. – icarus Feb 12 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.