0

can u guys tell why my script doesn't run, i have unexpected Syntax next to the symbole

#!/bin/sh
dused=$(df -h /home |awk '{print $5}'|sed 's/%//g')
used="${dused:0-1-2}"
echo $used
if [ $used -ge 50 ];
  then
    echo "The disk storage is used ${dused}%!"|mail -s "Alert: Disk Storage" xxxx@xxx.fr.
fi

the goal is to monitor storage, and recieve an email notification when its critical

thank you.

i tired multiple things, but still have the same probleme

Ookmai
  • 21
  • 4
  • 3
    If OK in your environment, change `#!/bin/sh` to `#!/bin/bash` Also copy/paste your code as is into https://shellcheck.net and fix any errors flaged there. In the future, please make `shellcheck` your first point of testing. Good luck. – shellter Feb 08 '23 at 17:03
  • 1
    bash != sh. Your syntax is almost ok for bash, see @shellter's comment. – Nic3500 Feb 08 '23 at 17:57
  • 1
    See [Difference between sh and Bash](https://stackoverflow.com/q/5725296/4154375). – pjh Feb 08 '23 at 17:58
  • 1
    What is the *exact* and full error that you get? See [this](https://meta.stackoverflow.com/questions/359146/why-should-i-post-complete-errors-why-isnt-the-message-itself-enough/359147#359147) for some examples of the importance of reporting exact error messages. – Gordon Davisson Feb 08 '23 at 18:08
  • @shelter, i changed the "sh to bash" and still have the same probleme, i also checked the Shellcheck.net to correct the script, and it said that its ok. – Ookmai Feb 09 '23 at 08:30
  • @GordonDavisson, the exact error is "storage.sh: ligne 8: erreur de syntaxe près du symbole inattendu « fi »". it is in french, – Ookmai Feb 09 '23 at 08:33
  • @Ookmai That may mean there are weird (maybe invisible) characters in the file, probably on the line with `then`. Try printing the script with `LC_ALL=C cat -v scriptname.sh` and see if anything strange appears. Note that it is normal for any accented characters in the text to be turned into cryptic sequences ("`è`" would be turned into "`M-CM-(`"), it's only if things like that appear where the plain bash code should be that indicates a problem. – Gordon Davisson Feb 09 '23 at 09:43
  • @GordonDavisson, i dont see any speciale caractere using the commande u gave me the result is : #!bin/sh^M dused=$(df -h /home|awk '{print $5}'|sed 's/%//g')^M used="${dused:0-1-2}"^M echo $used^M if [ $used -ge 50 ];then^M echo "The disk is full"|mail -s "Alert: disk storage" xxxx@xxxx.fr.^M fi – Ookmai Feb 09 '23 at 10:26
  • Those `^M` characters are the problem -- they're carriage returns, and they indicate that the script has DOS/Windows-style line endings, rather than unix-style. You need to convert the script to unix line endings. See ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) for more info and methods to do the conversion. – Gordon Davisson Feb 09 '23 at 10:50
  • @GordonDavisson, its working, thank you, mail services dosn't work, but its not the subject or the current question. thank you. we can close the question – Ookmai Feb 09 '23 at 11:29

0 Answers0