0
#!/bin/sh
VAR2=0
while [ $VAR2 -eq 0 ]: do
    echo "Please choose one of the following options:"
    echo "1. List the current running processes"
    echo "2. Check the available free memory"
    echo "3. List the disks/partitions"
    echo "4. Check for hardware (PCI)"
    echo "5. Check for package installation"
    echo "6. Create multiple files"
    echo "7. Remove multiple files"
    echo "8. List the contents of the current directory"
    echo "0. Exit"
    read VAR1
    if [ $VAR1 -eq 0 ]; then
        VAR2=2
    fi
    if [ $VAR1 -eq 1 ]; then
        $(top)
    fi
    if [ $VAR1 -eq 2 ]; then
        $(free)
    fi
    if [ $VAR1 -eq 3 ]; then
        $(df)
    fi
    if [ $VAR1 -eq 4 ]; then
        echo "Insert the name of the hardware that you want to search:" read VARHARD $(sudo lspci | grep $VARHARD)
    fi
    if [ $VAR1 -eq 5 ]; then
        echo "Insert the name of the package that you want to search:" read VARPACK $(rpm -qa | grep VARPACK)
    fi
    if [ $VAR1 -eq 6 ]; then
        echo "Insert the base name of the files:" read VARFILE echo "Insert the amount of files you want:" read VARNUMB $(touch $VARFILE{0001..000$VARNUMB})
    fi
    if [ $VAR1 -eq 7 ]; then
        echo "Insert a string to delete all files that contain it:" read VARDEL $(find -type f -name '*$VARDEL*' -exec rm {} \;)
    fi
    if [ $VAR1 -eq 8 ]; then
        $(ls -la)
    fi
    echo "Press any key and enter to continue... "
    read teste
    done

So, when I try to run the script "sh script.sh", it gives me an error that says "Syntax error near unexpected token `token'"

Can someone explain the error to me please? I'm new on scripting.

Thanks!

  • It's unlikely to solve your problem but consider using `elif` instead of `fi` followed by `if`. Alternatively you could use `case` statement. Are you by any chance running a script that you wrote on Windows? – Tom Fenech Jan 10 '19 at 15:18
  • Yes, I wrote the code on windows and then passed it to linux – Sergiy Khomyn Jan 10 '19 at 15:24
  • Possible duplicate of [unable to solve "syntax error near unexpected token \`fi'" - hidden control characters (CR) / Unicode whitespace](https://stackoverflow.com/questions/41100457/unable-to-solve-syntax-error-near-unexpected-token-fi-hidden-control-chara) – Tom Fenech Jan 10 '19 at 15:26
  • just checked it and it had no errors – Sergiy Khomyn Jan 10 '19 at 15:35
  • Sergiy Khomym you had a typo, look at my answer :) – Ass3mbler Jan 10 '19 at 15:46

2 Answers2

0

Why are you using the syntax $(top)? That will execute top to completion (it may be long running, and never end), and then evaluate the output as a command and attempt to execute it. Most likely, the output of top is not valid shell syntax. I'm not sure exactly which command is generating the syntax error related to token, but that's probably the source of your error. Instead of $(top), just write top. Same for all the other instances of $() in the script.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

You have two problems in your code, the first is the invocation of subshells where is not due (using $() ) and the second is a typo at the line 26 (you have if instead of fi). The following corrected code works:

#!/bin/bash
VAR2=0
while [ $VAR2 -eq 0 ]; do
 echo "Please choose one of the following options:"
 echo "1. List the current running processes"
 echo "2. Check the available free memory"
 echo "3. List the disks/partitions"
 echo "4. Check for hardware (PCI)"
 echo "5. Check for package installation"
 echo "6. Create multiple files"
 echo "7. Remove multiple files"
 echo "8. List the contents of the current directory"
 echo "0. Exit"
 read VAR1
 if [ $VAR1 -eq 0 ]; then
      VAR2=2
 fi
 if [ $VAR1 -eq 1 ]; then
      top
 fi
 if [ $VAR1 -eq 2 ]; then
      free
 fi
 if [ $VAR1 -eq 3 ]; then
      df
 fi
 if [ $VAR1 -eq 4 ]; then
      echo "Insert the name of the hardware that you want to search:"
      read           VARHARD
      sudo lspci | grep $VARHARD
 fi
 if [ $VAR1 -eq 5 ]; then
      echo "Insert the name of the package that you want to search:"
      read VARPACK
      rpm -qa | grep VARPACK
 fi
 if [ $VAR1 -eq 6 ]; then
      echo "Insert the base name of the files:"
      read VARFILE
      echo "Insert the amount of files you want:"
      read VARNUMB
      touch $VARFILE{0001..000$VARNUMB
 fi
 if [ $VAR1 -eq 7 ]; then
      echo "Insert a string to delete all files that contain it:"
      read VARDEL
      find -type f -name '*$VARDEL*' -exec rm {} \;
 fi
 if [ $VAR1 -eq 8 ]; then
      ls -la
 fi
 echo "Press any key and enter to continue... "
 read teste
done
Ass3mbler
  • 3,855
  • 2
  • 20
  • 18