Find the answer to your Linux question:
Results 1 to 6 of 6
On my laptop, when I close the lid it goes into hibernation. When I reopen the lid, I am no longer connected to the internet. I am currently fixing this ...
  1. #1
    Just Joined!
    Join Date
    Jun 2007
    Posts
    9

    Help with bash script

    On my laptop, when I close the lid it goes into hibernation. When I reopen the lid, I am no longer connected to the internet. I am currently fixing this problem using the following
    script.

    rm /etc/dhcpc/dhcpcd-eth0.pid; dhcpcd

    This works very well except that after a few times of doing this, I have dhcpcd running on a bunch of different processes. So, I started working on a new script. I'm pretty new to linux and bash and I can't figure out why this script won't run. It throws a syntax error near unexpected token ';'. Any help would be great, and thanks in advance. The following is the new script.

    rm /etc/dhcpc/dhcpcd-eth0.pid;
    ps > txt.txt;
    grep dhcpcd txt.txt > txt1.txt;
    cut -c1-5 txt1.txt > txt2.txt;
    rm txt.txt txt1.txt;
    for i in $(cat txt2.txt);
    do kill $i;
    dhcpcd

  2. #2
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    I think it's because you forgot to include a "done" after your "do".
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

  3. #3
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    Just to let you know that you can compress a lot of this. For example,
    Code:
    ps > txt.txt;
    grep dhcpcd txt.txt > txt1.txt;
    cut -c1-5 txt1.txt > txt2.txt;
    Could become
    Code:
    ps aux | grep dhcpcd | grep -v grep | awk '{print $2}' > txt2.txt
    That will strip out any processes running that include the word "dhcpcd" but *not* the word "grep", print the second word on that line (i.e. the pid), and dump it to txt2.txt.

    EDIT: You could even include all that (without "> txt2.txt" of course) inside the brackets like this,
    Code:
    for i in $(ps aux | grep dhcpcd | grep -v grep | awk '{print $2}') ; do kill $i ; done
    A one line bash-script!
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    here's another version
    Code:
    ps aux | awk '/dhcpcd/{print $2}' | xargs kill

  5. #5
    Just Joined!
    Join Date
    Jun 2007
    Posts
    9
    Thanks for the replies, I got it working.It was because of the missing 'done'. Thanks also for the other examples. I am pretty new to bash so I will definitely check them out to see what makes them tick! Thanks alot!

  6. #6
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    Quote Originally Posted by ghostdog74 View Post
    here's another version
    Code:
    ps aux | awk '/dhcpcd/{print $2}' | xargs kill
    That's much neater than my idea. xargs is such an incredibly useful command!
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

Posting Permissions

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