Unix/Linux Go Back    


Homework & Coursework Questions Students must use and complete the template provided. If you don't, your post may be deleted! Special homework rules apply here.

Help with shell scrip syntax errors

Homework & Coursework Questions


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-15-2016
Demas Demas is offline
Registered User
 
Join Date: Oct 2016
Last Activity: 15 October 2016, 2:43 PM EDT
Posts: 7
Thanks: 6
Thanked 0 Times in 0 Posts
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
Code:
Geral
SO
PCD
FBD

registrations.txt
Code:
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.

2. Relevant commands, code, scripts, algorithms:



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


Code:
#!/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



4. 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).

Last edited by rbatte1; 10-17-2016 at 08:20 AM.. Reason: Converted to formatted number-list and added CODE tags for file contents
Sponsored Links
    #2  
Old Unix and Linux 10-15-2016
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 January 2017, 12:29 PM EST
Location: Aachen, Germany
Posts: 10,159
Thanks: 230
Thanked 3,073 Times in 2,849 Posts
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.

Last edited by RudiC; 10-15-2016 at 10:10 AM..
The Following User Says Thank You to RudiC For This Useful Post:
Demas (10-15-2016)
Sponsored Links
    #3  
Old Unix and Linux 10-15-2016
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 January 2017, 7:43 AM EST
Location: NM
Posts: 10,930
Thanks: 474
Thanked 1,005 Times in 934 Posts
Lets start with basic syntax -
use ;; after each case block
For every if there must be a fi

Some changes are in red


Code:
#!/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

The Following User Says Thank You to jim mcnamara For This Useful Post:
Demas (10-15-2016)
    #4  
Old Unix and Linux 10-15-2016
Demas Demas is offline
Registered User
 
Join Date: Oct 2016
Last Activity: 15 October 2016, 2:43 PM EDT
Posts: 7
Thanks: 6
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by jim mcnamara View Post
Lets start with basic syntax -
use ;; after each case block
For every if there must be a fi

Some changes are in red


Code:
#!/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

Thank you! I added all you said and I keep getting this error: end of file unexpected (expecting "fi")
Sponsored Links
    #5  
Old Unix and Linux 10-15-2016
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 January 2017, 12:29 PM EST
Location: Aachen, Germany
Posts: 10,159
Thanks: 230
Thanked 3,073 Times in 2,849 Posts
What I get is

Code:
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"
The Following User Says Thank You to RudiC For This Useful Post:
Demas (10-15-2016)
Sponsored Links
    #6  
Old Unix and Linux 10-15-2016
Demas Demas is offline
Registered User
 
Join Date: Oct 2016
Last Activity: 15 October 2016, 2:43 PM EDT
Posts: 7
Thanks: 6
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by RudiC View Post
What I get is

Code:
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"
Thank you! I already added here the channels.txt and registrations.txt
Sponsored Links
    #7  
Old Unix and Linux 10-15-2016
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 January 2017, 12:29 PM EST
Location: Aachen, Germany
Posts: 10,159
Thanks: 230
Thanked 3,073 Times in 2,849 Posts
How did you add them?

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

won't work, no way!
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Seek help on shell script syntax errors duke0001 Emergency UNIX and Linux Support 8 08-03-2011 04:06 AM
Get file name in shell scrip loop: bad substitution soueric Solaris 4 05-03-2011 12:20 AM
how to combine two files into one file using shell scrip netbanker Shell Programming and Scripting 2 12-06-2010 06:34 PM
How to convert shell scrip to binaric command raghunsi UNIX for Advanced & Expert Users 1 02-03-2010 02:27 AM
Using cp command inside shell scrip yoavbe Shell Programming and Scripting 2 06-22-2006 10:57 AM



All times are GMT -4. The time now is 02:13 PM.