Page 1 of 2 12 LastLast
Results 1 to 25 of 27
  1. #1
    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"?

  2. #2
    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.

    Shikha
    Dinsol.com Web Hosting Since Year 1999 Open Running Stable
    Social Media Optimization & Marketing Services with World Class Quality
    www.dinsol.com

  3. #3
    Join Date
    May 2001
    Posts
    1,513
    Thanks, but I don't have pico installed.

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

  5. #5
    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?
    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).

    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"?
    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...
    Alex Llera
    Professional Server Management
    FreeBSD|Linux|HSphere|Cpanel|Plesk

  6. #6
    Join Date
    Mar 2002
    Location
    St. Louis, MO
    Posts
    1,379
    i use vi

  7. #7
    Join Date
    Jun 2000
    Location
    Alabama of course
    Posts
    2,247
    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

  8. #8
    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...
    I couldn't get the above command or this cat miniserv.users | sed -e 's/adm/admin/g' > /tmp/miniserv.users.new
    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.

  9. #9
    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???

  10. #10
    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

  11. #11
    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. #12
    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.

  13. #13
    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".
    I was wanting to know a shell command that will insert text either at the beginning or at the end of the file, or stick it in an empty file.

    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.

  14. #14
    'pico' is very nice editor and I would recommend it. It is not as complicated as 'vi' is.
    I like it also.

    Shikha
    Dinsol.com Web Hosting Since Year 1999 Open Running Stable
    Social Media Optimization & Marketing Services with World Class Quality
    www.dinsol.com

  15. #15
    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??

  16. #16
    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.

  17. #17
    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.

  18. #18
    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.

  19. #19
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    75
    Originally posted by chrisb
    login user | echo user_password
    I am not sure if that will work and if it works than you should enter plaintext password (not encrypted) which is a very bad idea as the whole command including the password will be saved in your .bash_history file.

  20. #20
    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.

  21. #21
    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".
    To insert text at beginning of a file:

    Code:
    (echo text ; cat file ) > file.tmp ; mv file.tmp file

  22. #22
    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.
    It seems like tcl and expect installed just fine, but I haven't got your script above to run from the web. I chmodded it to 755 (I'm running suEXEC on linux) and put it in my cgi-bin. I even tried adding a -f, such as #!/usr/bin/expect -f, and still get a technical error. Then, I tried adding a content header, and still a technical error. I'd like to run the script from the web. How do I do that?

    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.

  23. #23
    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."

  24. #24
    Join Date
    Aug 2002
    Location
    Baltimore, Maryland
    Posts
    580
    pico -w file

  25. #25
    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •