Help with shell scrip syntax errors

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

This script will analyse the channels.txt e registrations.txt and it will allow to mage the channels and the users registrations on the channels. It should look similar to this:

Channel administration of SlackIUL:

  1. Channels list
  2. Add channel
  3. Remove channel
  4. Associate user to channel
  5. Channel users list
  6. Remove channel user
  7. Exit

Always that justifies, the script should ask o the users name and/or channel name.

channels.txt

Geral
SO
PCD
FBD

registrations.txt

2016-09-26 14:00:01 paulo Geral
2016-09-26 14:01:11 paula SO
2016-09-27 10:33:17 paulo SO
2016-09-27 13:32:10 rita Geral
2016-09-27 13:32:12 rita FBD

I've tried doing it but there is always some syntax error.

  1. Relevant commands, code, scripts, algorithms:

  2. The attempts at a solution (include all code and scripts):

#!/bin/bash
	echo "-----------------------------------"
	echo "Administration options of SlackIUL"
	echo "1. Channels list"
	echo "2. Add channel"
	echo "3. Remove channel"
	echo "4. Associate user to channel"
	echo "5. Channel users list"
	echo "6. Remove user from channel"
	echo "7. Exit"
	read x
	case $x in
        1)	cat channels.txt ;;
        2)	echo "Say the channel to add:"
			read channel_add
				if [ grep -q $channel_add 'channels.txt' == 0 ]; then
					echo "Channel already exists"
				else
			                echo "$channel_add" >> "channels.txt"
			        fi
	3)	echo "Say channel to remove:"
			read channel_remove
				if [ grep -q $channel_remove 'channels.txt' == 0 ]; then
					echo "$channel_remove" << "channels.txt"
				else
			                echo "The channel you want to remove, does't exist."
			        fi
        4)	
	5)	echo "Say the channel from wich to list users:"
			read channel
				if [ cat registrations.txt | grep $channel ]; then
					cat registrations.txt | grep $channel | cut -d ' ' -f3
				else 
					echo "The channel doesn't exist!"
				fi
	6)	echo "Say the channel:"
			read channel
		echo "Say the user to remove:"
			read user
				if [ cat registrations.txt | grep $user | grep $channel ]; then
					line=$(cat -n registrations.txt | grep $user | grep $channel)
					echo "$line" << "registrations.txt"
	7)	echo "Till next time :)" ;;
	*)	echo "Invalid option"
			x=0;;
	esac
done
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

ISCTE, Lisbon, Portugal, Fernando Batista, Licenciatura em Engenharia Inform�tica (9119) [ISTA]

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Hello Demas, welcome to unix.com!

In your school data the course number is missing. Please complete.

Please be aware that supplying sample data like channels.txt and registrations.txt usually helps, as does posting the syntax error messages.
On first sight, I see the following:

  • missing double semicolons on a few of the cases.
  • << opens a "here document", the syntax (and logics) for which you do not follow. It seems you would need an output redirection in those places.
  • grep et al. don't need cat - they usually read the input files by themselves.
1 Like

Lets start with basic syntax -
use ;; after each case block
For every if there must be a fi

Some changes are in red

#!/bin/bash
	echo "-----------------------------------"
	echo "Administration options of SlackIUL"
	echo "1. Channels list"
	echo "2. Add channel"
	echo "3. Remove channel"
	echo "4. Associate user to channel"
	echo "5. Channel users list"
	echo "6. Remove user from channel"
	echo "7. Exit"
	read x
	case $x in
        1)	cat channels.txt ;;
        2)	echo "Say the channel to add:"
			read channel_add
				if [ grep -q $channel_add 'channels.txt' == 0 ]; then
					echo "Channel already exists"
				else
			                echo "$channel_add" >> "channels.txt"
			  fi ;;
	3)	echo "Say channel to remove:"
			read channel_remove
				if [ grep -q $channel_remove 'channels.txt' == 0 ]; then
					echo "$channel_remove" << "channels.txt"
				else
			                echo "The channel you want to remove, does't exist."
			  fi ;;
  4)	
	5)	echo "Say the channel from wich to list users:"
			read channel
				if [ cat registrations.txt | grep $channel ]; then
					cat registrations.txt | grep $channel | cut -d ' ' -f3
				else 
					echo "The channel doesn't exist!"
				fi ;;
	6)	echo "Say the channel:"
			read channel
		echo "Say the user to remove:"
			read user
				if [ cat registrations.txt | grep $user | grep $channel ]; then
					line=$(cat -n registrations.txt | grep $user | grep $channel)
					echo "$line" << "registrations.txt"
		  fi;;			
	7)	echo "Till next time :)" ;;
	*)	echo "Invalid option"
			x=0;;
	esac
done
1 Like

Thank you! I added all you said and I keep getting this error: end of file unexpected (expecting "fi")

What I get is

bash: warning: here-document at line 24 delimited by end-of-file (wanted `channels.txt')
bash: file4: line 49: syntax error: unexpected end of file

So - take into account the remark on "here documents"

1 Like

Thank you! I already added here the channels.txt and registrations.txt

How did you add them?

echo "$channel_remove" << "channels.txt"

won't work, no way!

 sed -E "s/$channel_remove//g"

Surely to remove a channel, you need to

grep -v "$channel_remove" channels.txt > channels.NEW
mv channels.NEW channels.txt

ps - just noticed the sed command that Demas had posted which will answer this query too.

Take note of the last line, done .
Something is missing, for example...

while true
do
        lots of code here
done        #Your line 48 as 49 does not technically exist.