Help with shell scrip syntax errors

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Help with shell scrip syntax errors
# 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 08: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 10:10 AM..
This User Gave Thanks to RudiC For This Post:
# 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

This User Gave Thanks to jim mcnamara For This Post:
# 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"
This User Gave Thanks to RudiC For This Post:
# 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 or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Shell Scrip in Masking particular columns in .csv file or .txt file using shell script

Hello Unix Shell Script Experts, I have a script that would mask the columns in .csv file or .txt file. First the script will untar the .zip files from Archive folder and processes into work folder and finally pushes the masked .csv files into Feed folder. Two parameters are passed ... (5 Replies)
Discussion started by: Mahesh G
5 Replies

2. Emergency UNIX and Linux Support

Seek help on shell script syntax errors

I want to delete archivelog files that has been archived and applied from primary database to standby database. This piece of script is working in Linux server. However, I copy it to Unix server with tiny modification. It won't work and generate the error message. I have checked code carefullt... (8 Replies)
Discussion started by: duke0001
8 Replies

3. Solaris

Get file name in shell scrip loop: bad substitution

Hi guys. Good day, morning, afternoon or night, depending on where you live. I have a script shell in which I am looping on files (absolute path) see code section above. I always have an error: bad substitution. :wall: Is it because my variable file is the index of the loop and not a normal... (4 Replies)
Discussion started by: soueric
4 Replies

4. Shell Programming and Scripting

how to combine two files into one file using shell scrip

eg. file 1 has: 4 0 8628380 653253 0 0 0 0 0 0 2 0 8626407 655222 0 0 0 0 0 0 4 0 8633729 647892 0 0 0 0 0 0 5 0 8646253 635367 0 0 0 0 0 0 file 2 has: 4798 48717 11554 5408 56487 14359 6010 58415 15220 5541 41044... (2 Replies)
Discussion started by: netbanker
2 Replies

5. Shell Programming and Scripting

line count with if /else - syntax errors

this is the script: ps -ef|grep "x_jobstat 10 v001" > jobstatv001.txt ps -ef |grep "x_jobserver 10 v001" >> jobstatv001.txt #Sample text - each line preceded by 4 spaces # root 133064 102986 0 08:49:28 pts/6 0:00 grep x_jobstat 10 v001 # root 137550 1 0 Nov 08 - 0:28... (6 Replies)
Discussion started by: kwalkner
6 Replies

6. Shell Programming and Scripting

PERL Syntax Errors

Hi, I am a newbie to PERL and working on a script. When running it I get a lot of compilation errors. The actual command in the program (which is within a case structure) is given below # This gives the actual count of inquires from a log file (It works fine when I type this on the... (2 Replies)
Discussion started by: nurani
2 Replies

7. UNIX for Advanced & Expert Users

How to convert shell scrip to binaric command

here is a typical question for everyone ... How to convert a given shell script to binaric command along with its shell script switches. Any !dea (1 Reply)
Discussion started by: raghunsi
1 Replies

8. Shell Programming and Scripting

Using cp command inside shell scrip

Hi, First i would like to say that im a unix begginer. I have a file named /tmp/sample.lst that contain about 20 rows like the following two : '/tmp/aa.txt' '/temp/aa.txt' '/tmp/xx.txt' '/temp/xx.txt' Inside a ksh script i would like to do the following task: add the cp command at... (2 Replies)
Discussion started by: yoavbe
2 Replies

9. Shell Programming and Scripting

AWK Syntax errors :/

I recently started as an intern and my manager wanted to see how well I would handle Korn Bourne shell scripting without any prior experience, I have prior programming experience but I keep running into syntax errors with AWK. Please take a look at my simple code and tell me what stupid mistake... (6 Replies)
Discussion started by: yongho
6 Replies

10. Shell Programming and Scripting

Execute an Oracle stored procedure from a shell scrip

Here is a snippet of my code: if then echo "\n Deleting all reports older than 24 hours. \n" >> $logfile ls -l $FileName >> $logfile ... (1 Reply)
Discussion started by: mh53j_fe
1 Replies
Login or Register to Ask a Question