👤
Home Man
Search
Today's Posts
Register

Students must use and complete the template provided. If you do not use the template your post may be deleted! Special homework rules apply here.

Help with shell scrip syntax errors

👤 Login to reply

 
Thread Tools Search this Thread
# 1  
Old 10-15-2016
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 07:20 AM.. Reason: Converted to formatted number-list and added CODE tags for file contents
# 2  
Old 10-15-2016
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 09:10 AM..
The Following User Says Thank You to RudiC For This Useful Post:
Demas (10-15-2016)
# 3  
Old 10-15-2016
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 10-15-2016
Quote:
Originally Posted by jim mcnamara
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")
# 5  
Old 10-15-2016
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)
# 6  
Old 10-15-2016
Quote:
Originally Posted by RudiC
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
# 7  
Old 10-15-2016
How did you add them?
Code:
echo "$channel_remove" << "channels.txt"

won't work, no way!
👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with Shell Scrip in Masking particular columns in .csv file or .txt file using shell script Mahesh G Shell Programming and Scripting 5 11-06-2016 11:59 PM
Seek help on shell script syntax errors duke0001 Emergency UNIX and Linux Support 8 08-03-2011 03:06 AM
Get file name in shell scrip loop: bad substitution soueric Solaris 4 05-02-2011 11:20 PM
how to combine two files into one file using shell scrip netbanker Shell Programming and Scripting 2 12-06-2010 05:34 PM
PERL Syntax Errors nurani Shell Programming and Scripting 2 06-16-2010 12:49 AM
perl version for syntax errors bigben1220 Shell Programming and Scripting 13 05-12-2010 06:11 AM
How to convert shell scrip to binaric command raghunsi UNIX for Advanced & Expert Users 1 02-03-2010 01:27 AM
Using cp command inside shell scrip yoavbe Shell Programming and Scripting 2 06-22-2006 09:57 AM
AWK Syntax errors :/ yongho Shell Programming and Scripting 6 06-08-2005 02:08 PM
Execute an Oracle stored procedure from a shell scrip mh53j_fe Shell Programming and Scripting 1 06-03-2005 03:17 PM


All times are GMT -4. The time now is 02:28 AM.

Unix & Linux Forums Content CopyrightŠ1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password