LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2015, 10:06 AM   #1
hadarg
LQ Newbie
 
Registered: Mar 2015
Posts: 1

Rep: Reputation: Disabled
Bash script with sed


Hi,
I wrote a script with 3 parameters: adduser, searchuser and deleteuser and for some weird reason the deleteuser doesn't work. can someone assist me with this problem?

Thanks!!

function deleteuser ()
{
if grep -q "$1" "$usersfile"; then
sed -i '/$1/d' "$usersfile"
echo $?
echo "User was deleted"
return
else
echo "User wasn't found"
return
fi
}
 
Old 03-07-2015, 03:52 PM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
How about
Code:
delete_user () {
	grep -q "$1" "$usersfile"
		if [ $? -eq 0 ]; then
			sed -i '/$1/d' "$usersfile"
			echo "User was deleted"
			else 
			echo "User wasn't found"
		fi
	return
}
 
Old 03-08-2015, 07:20 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Firstly, no need to use $? as suggested as the current solution already has 'if' do that.

Your function uses $1 and $usersfile, where is the second being set as it is not done in the function?

Also, no need to return twice (if at all).

Finally, would you explain what - deleteuser doesn't work - means? This is an ambiguous statement. Also, have you isolated this function into a script on it own and tested it?
 
Old 03-10-2015, 07:47 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
A good thing to add to this is to enable debugging within your script, "set -xv" at the top of your script, but below the #!/bin/sh line will turn on verbose output and show you the actions and decisions made as your script runs. It will help you to debug things.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed command from bash script gushnik1 Linux - Software 6 09-18-2013 02:45 AM
[SOLVED] bash script sed -r -i and -e parameters charu Programming 10 05-06-2011 06:28 AM
[SOLVED] bash script and sed Snoken99 Linux - Newbie 7 03-14-2011 10:05 AM
[SOLVED] Help with sed script in bash corp769 Programming 5 02-26-2011 03:27 PM
problem with sed in a bash script nexus55 Linux - Software 6 05-03-2004 09:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration