Results 1 to 25 of 27
Thread: Shell Command Help Needed
-
12-15-2002, 08:52 AM #1Disabled
- Join Date
- May 2001
- Posts
- 1,513
Shell Command Help Needed
I know many shell commands, but am trying to learn how to edit a file using a shell editor. Which one is the simplest? ed? less? vi?
I've read the man pages for vi, ed, and less and still am not getting any of the editors to write my changes a file. Here's one of the syntaxes I've tried
vi miniserv.users ,s/adm/admin/g
So, what would the shell command be to change "adm" to "admin" within a file called "miniserv.users"?
-
12-15-2002, 09:14 AM #2Web Hosting Master
- Join Date
- Oct 2002
- Posts
- 724
You can use pico command to edit files. More you can get by man pico. I use this command sometime to update files on my Server.
Thanks.
ShikhaDinsol.com Web Hosting Since Year 1999 Open Running Stable
Social Media Optimization & Marketing Services with World Class Quality
www.dinsol.com
-
12-15-2002, 09:21 AM #3Disabled
- Join Date
- May 2001
- Posts
- 1,513
Thanks, but I don't have pico installed.
-
12-15-2002, 10:55 AM #4Aspiring Evangelist
- Join Date
- Jul 2002
- Location
- Israel
- Posts
- 351
cat miniserv.users | sed -e 's/adm/admin/g' > /tmp/miniserv.users.new
Take a look at the new file -- if it's okay, then simply move it over the old one.C#/C++/TCL/Python/PHP developer.
mark at mark org il
-
12-15-2002, 11:55 AM #5Web Hosting Master
- Join Date
- Apr 2001
- Location
- Palm Beach, FL
- Posts
- 1,097
Re: Shell Command Help Needed
Originally posted by chrisb
I know many shell commands, but am trying to learn how to edit a file using a shell editor. Which one is the simplest? ed? less? vi?
I've read the man pages for vi, ed, and less and still am not getting any of the editors to write my changes a file. Here's one of the syntaxes I've tried
vi miniserv.users ,s/adm/admin/g
So, what would the shell command be to change "adm" to "admin" within a file called "miniserv.users"?
sed s/adm/admin/g miniserv.users > miniserv.users.new
I rarely use sed, so I have to fiddle with the syntax until I get it right, but that should be fairly close...Alex Llera
Professional Server Management
FreeBSD|Linux|HSphere|Cpanel|Plesk
-
12-15-2002, 02:44 PM #6Web Hosting Master
- Join Date
- Mar 2002
- Location
- St. Louis, MO
- Posts
- 1,379
i use vi
-
12-15-2002, 02:57 PM #7
pico or jed I'd say would be the most user friendly... like they said.. if you don't have em get em, their in RPM format and easy to install... I can't really think of any other editor thats going to be easy to learn like those two...
KnownHost Managed Services Specialists
Fully Managed WebSite Hosting
Offering WordPress, Shared, Reseller, VPS, KVM, WordPress, Dedicated servers and more!
Contact us: sales@knownhost.com or by phone 1-866-332-9894
-
12-15-2002, 08:54 PM #8Disabled
- Join Date
- May 2001
- Posts
- 1,513
Re: Re: Shell Command Help Needed
Originally posted by allera
The simplest would be pico. If you don't have it, install it. Just download the tarball from www.washington.edu/pine and install pico alone (the instructions tell you how).
Try 'man sed', but it goes something like:
sed s/adm/admin/g miniserv.users > miniserv.users.new
I rarely use sed, so I have to fiddle with the syntax until I get it right, but that should be fairly close...
to work. Somehow my content is wiped out to 0 in the file. Don't know which command did it since I've tried so many.
That's OK. I just have to insert now instead of replacing.
Here's one of the commands I've tried, and it didn't work:
sed -e 'i\admin::0' miniserv.users
Thanks for the pico suggestion. I may install it later, but I'd like to get something working without having to do that. On my VPS, they don't have pico, NEdit or emacs installed.
I had only heard of the shell editors, pico, vi, and emacs previously. I had no idea that you could edit with less, or that there were so many other shell editors. Ed, vi, etc. all seem similar to the "ex" editor. However, sed seems different as it's referred to as a stream editor.
Thanks for refering me to sed. I'm trying to edit without a terminal and with a one line shell command, so "sed" looks like what I need. When I tried vi, I got these crazy .swp (swap) files written to a buffer.
It's the syntax that some of these shell programs call for that I'm having a hard time with, and those man and --help pages with percents, underlines, colons, backslashes, etc. confuse me. It's simple when you just need a hyphenated option such as ping -c1 yahoo.com, or a simple command like dig yahoo.com; but others are confusing.
Well, back to study man sed, and fiddle, fiddle, fiddle.
-
12-16-2002, 04:49 AM #9Disabled
- Join Date
- May 2001
- Posts
- 1,513
Well, I figured out the syntax for inserting text in sed goes something like
sed -e 'a\ admin::0' miniserv.users > miniserv.users
but I'm now getting a "can't find End of File" error. Maybe that's because the file is empty???
-
12-18-2002, 02:43 AM #10Disabled
- Join Date
- May 2001
- Posts
- 1,513
BTW, the shell command
sed -e 's/adm/admin/g' miniserv.users worked fine, replacing 'adm' with 'admin' and wrote directly to the file 'miniserv.users' without having to copy.
I never did figure out how to insert text with sed though. I think I have the correct syntax now, but get an EOF error. IE, my sed command say to insert "testing" would be:
sed -e 'i\ testing' miniserv.users
-
12-18-2002, 11:05 AM #11Web Hosting Guru
- Join Date
- Jul 2001
- Location
- Toronto, ON.
- Posts
- 312
Insert text where? Beginning of file? End of file? Beginning of line? End of line? I know appending text is easy (just "echo <text> >> <file>"), and inserting text at the beginning of the line is easy (ie. "sed -e 's/^/<text>/g'"), but I'm not sure about inserting it at the beginning of a file. The end of a line is easy as well, ie. "sed -e 's/$/<text>/g".
-
12-18-2002, 11:33 PM #12Junior Guru Wannabe
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 75
'pico' is very nice editor and I would recommend it. It is not as complicated as 'vi' is.
Another more complete program wich will allow you do any file manipulation (copy, delete, rename, edit, search, permissions, etc) is 'mc' (midnight commander). It has a very nice shell GUI (ncurses I think). If you were ever using DOS it will remind you to 'nc' (norton commander), it is completely same.
Give it a try, you won't be dissapointed.
-
12-19-2002, 05:44 AM #13Disabled
- Join Date
- May 2001
- Posts
- 1,513
Originally posted by lotuslnd
Insert text where? Beginning of file? End of file? Beginning of line? End of line? I know appending text is easy (just "echo <text> >> <file>"), and inserting text at the beginning of the line is easy (ie. "sed -e 's/^/<text>/g'"), but I'm not sure about inserting it at the beginning of a file. The end of a line is easy as well, ie. "sed -e 's/$/<text>/g".
BTW, right after my post, I found out I was wrong about not having to copy the file to change it in sed. Thus, I had to do "sed -e 's/adm/admin/g' miniserv.users > miniserv.users in order to change the file... just like Marc suggested.
-
12-19-2002, 08:14 AM #14Web Hosting Master
- Join Date
- Oct 2002
- Posts
- 724
'pico' is very nice editor and I would recommend it. It is not as complicated as 'vi' is.
ShikhaDinsol.com Web Hosting Since Year 1999 Open Running Stable
Social Media Optimization & Marketing Services with World Class Quality
www.dinsol.com
-
12-19-2002, 10:02 AM #15WHT Addict
- Join Date
- Dec 2002
- Location
- Cyprus
- Posts
- 127
i use vi that's the one i have learned.
what about emacs.
is anyone using emas??
-
12-21-2002, 06:06 PM #16Disabled
- Join Date
- May 2001
- Posts
- 1,513
Does anyone know the shell syntax to bypass the prompt on a dumb terminal? For example, say I want to enter
login root password
without getting a prompt. IOW, how can I put all the arguments on one line and avoid a prompt? I've searched many days unsuccessfully to find this out.
-
12-22-2002, 04:27 AM #17Junior Guru Wannabe
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 75
I think 'expect' will do the job for you. Try 'man expect' or visit http://expect.nist.gov/
You'll need something among these lines:
#!/usr/bin/expect
spawn telnet localhost
expect "login:"
send "root\r"
expect "Password:"
send "mypass\r"
# your commands here or use 'interact' to get a prompt
#send "quit" # if not in interactive mode
Save this as 'mylogin' and start it as ./mylogin
Don't forget to chmod it or you will give root access to everybody.
-
12-22-2002, 04:35 AM #18Disabled
- Join Date
- May 2001
- Posts
- 1,513
ThanxAlot, bitel!
It seems that a
login user | echo user_password
seems like it should work. However, I guess the password needs to be entered encrypted (dangerous I know). Yet, when I tried my encrypted password from /etc/shadow file, it said "incorrect login", so maybe it is getting the password from PAM or somewhere else.
Another strange thing is that my VPS doesn't even have the unix crypt command. I could install it.
Well, I can't wait to try that expect example. I'll let you know how it works. Have a feeling I'll have to install expect.
-
12-22-2002, 04:48 AM #19Junior Guru Wannabe
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 75
Originally posted by chrisb
login user | echo user_password
-
12-22-2002, 05:21 AM #20Disabled
- Join Date
- May 2001
- Posts
- 1,513
Thanks.
Well, just as I "expected" , I have to install expect. I'm reading the docs now. I downloaded it from
http://expect.nist.gov/expect.tar.gz and untarred it to /usr/bin directory.
Looks like I need TCL installed first, so I'm off to do that.
-
12-22-2002, 05:44 AM #21Web Hosting Master
- Join Date
- Apr 2001
- Location
- Depok, Indonesia
- Posts
- 988
Originally posted by lotuslnd
Insert text where? Beginning of file? End of file? Beginning of line? End of line? I know appending text is easy (just "echo <text> >> <file>"), and inserting text at the beginning of the line is easy (ie. "sed -e 's/^/<text>/g'"), but I'm not sure about inserting it at the beginning of a file. The end of a line is easy as well, ie. "sed -e 's/$/<text>/g".
Code:(echo text ; cat file ) > file.tmp ; mv file.tmp file
-
12-22-2002, 08:53 AM #22Disabled
- Join Date
- May 2001
- Posts
- 1,513
Originally posted by bitel.biz
I think 'expect' will do the job for you. Try 'man expect' or visit http://expect.nist.gov/
You'll need something among these lines:
#!/usr/bin/expect
spawn telnet localhost
expect "login:"
send "root\r"
expect "Password:"
send "mypass\r"
# your commands here or use 'interact' to get a prompt
#send "quit" # if not in interactive mode
Save this as 'mylogin' and start it as ./mylogin
Don't forget to chmod it or you will give root access to everybody.
Also, from the shell, I tried ./mylogin and I get "Permission Denied"; but if I do "expect mylogin" it sorta works and waits for me to enter the password, even though I put the password in the file.
-
12-22-2002, 09:57 AM #23Aspiring Evangelist
- Join Date
- Oct 2002
- Location
- Tel-Aviv, Israel
- Posts
- 436
you can run
(echo "password"|login <username> -p)
then when login goes you'll have the file .bash_profile executed. so you can edit that file to do what ever you want.Last edited by rcs; 12-22-2002 at 10:07 AM.
Uadm.com - Unix Administration, Security and Support.
http://www.uadm.com
"Unix is user friendly; it's just picky about who its friends are."
-
12-22-2002, 01:25 PM #24Web Hosting Master
- Join Date
- Aug 2002
- Location
- Baltimore, Maryland
- Posts
- 580
pico -w file
-
12-22-2002, 06:18 PM #25Hail Eris !
- Join Date
- Oct 2002
- Location
- Canada
- Posts
- 3,103
Not sure what are you trying to achive, but if you have to "telnet" into accounts you should use ssh and not telnet. Look at ssh docs and identity file. No passwords should fly around.