Find the answer to your Linux question:
Results 1 to 5 of 5
Hi all, I am a newbie to this linux game. Actually a student at University, and one module i am undertaking is Unix Systems Admin. Found it hard, but starting ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2012
    Posts
    2

    Angry Trying to write a shell script!!!!!!! (with no success)


    Hi all,

    I am a newbie to this linux game. Actually a student at University, and one module i am undertaking is Unix Systems Admin. Found it hard, but starting to enjoy it somewhat............until i encountered this:

    I am tasked with creating a shell script, called terminador, that terminates all processess of a certain name which is given to the shell script as arguement. (guidance: pidof; ps -ef; awk)

    now this is what i have so far...............

    #!/bin/bash
    process=`pgrep -U root "$1" | wc -l`
    if [ $process -ge 1 ]; then
    echo 'You are unable to kill this process'
    else
    pkill $1
    echo $1 ‘The process has been TERMINADED’
    fi

    could someone explain to me what i am doing wrong. I keep getting different error messages. line 3 syntax error, i change it, or so i think, and get another one. line 6 syntax error, i change that, and get another one, and its never ending. Not sure where i am going wrong.

    HELP!!!! pleeeeeeeeeeeeeeeeeease!!!!!

  2. #2
    Linux Enthusiast Mudgen's Avatar
    Join Date
    Feb 2007
    Location
    Virginia
    Posts
    664
    I don't see any syntax errors from a bash standpoint, although those open-quote and close-quote characters you're using on the second echo command are kind of weird. Try replacing them with ordinary single or double quotes.

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Location
    Cambridge, Ontario, Canada
    Posts
    24
    I just copied and pasted your program into a script on my machine and ran it with no syntax errors. I was also able to remove those quotes around "$1" on line 2 without any change in the program's output. It's also worth noting that when one gives a jibberish argument, the output will say that the program is terminated. If this assignment of yours is done, please repost in this thread saying so. Good luck!

  4. $spacer_open
    $spacer_close
  5. #4
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    Pro-tip: ALWAYS quote your variables in bash. For example, your "process" variable is not quoted. What happens if this code returns an error:
    Code:
    pgrep -U root "$1" | wc -l
    The contents of "$process" will be an empty string. So when bash expands this line of code:
    Code:
    if [ $process -ge 1 ]; then
    it will expand to this code:
    Code:
    if [ -ge 1]; then
    which is a syntax error. If "pgrep | wc" did not fail, then "$process" will contain a non-empty string, and the if statement will not expand to a statement with bad syntax. That is why sometimes it might work, but other times it will fail with a syntax error.

    Bash is kind of stupid that way, it just dumps the contents of a string into a line, then re-evaluates that line. So in "bash", always use quotes:
    Code:
    if [ "$process" -ge 1 ]; then
    If the pgrep or wc process fail, and $process is an empty string, it will expand to this:
    Code:
    if [ "" -ge 1 ]; then
    which is not a syntax error, but it still might not do what you want.

  6. #5
    Just Joined!
    Join Date
    Apr 2012
    Posts
    2
    Thanks for the help folks. managed to get it fixed. Was me inputting a typo into script. syntax was ok. cheers for the help

Posting Permissions

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