Find the answer to your Linux question:
Page 1 of 4 1 2 3 4 LastLast
Results 1 to 10 of 34
Hi there. Bare with me as i am just teaching myself to do Linux scripting. This is what i want to do: I want to create a script that a) ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2006
    Posts
    16

    Scripting Help PLEASEEEEEEE!!!


    Hi there. Bare with me as i am just teaching myself to do Linux scripting. This is what i want to do:

    I want to create a script that

    a) take a command line argument of a users username
    b) test to make sure that a command line argument was provided.

    Anyone can start me off as i do not even know what a command line argument is?

    thanks

    krist

  2. #2
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,729
    hi kristb17 !!!

    is it a home work ???

    Anyone can start me off as i do not even know what a command line argument is?
    $ sh script1 hello user

    here "hello" and "user" are two arguments passed to script named script1
    you can access first as $1, second $2 and so on.....
    $# gives you the number of arguments passed to the script.....

    copy this script and execute....
    script name --- script1

    echo "no of arguments passed " $#
    echo "first argument" $1
    echo "second argument" $2

    save it and execute.....
    $ sh script1 hello kristb17

    <=== { casper } ===>
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  3. #3
    Just Joined!
    Join Date
    Aug 2006
    Posts
    16
    Hi there. Thanks again for your reply casper. But i think the main problem im having is the jargon. I dont understand the question. So does it mean i ask the user to type in a command line? i dont understand what the user must input. If you read the two questions below what do you think it should do?

    thanks




    a) take a command line argument of a users username
    b) test to make sure that a command line argument was provided

  4. $spacer_open
    $spacer_close
  5. #4
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,729
    hi kristb17 !!

    Quote Originally Posted by kristb17
    Anyone can start me off as i do not even know what a command line argument is?
    in last post, i explained what command line arguments are and how you can use these so that you can solve problem yourself.....

    a) take a command line argument of a users username
    user have to type username ...
    $ sh script1 kristb17

    b)test to make sure that a command line argument was provided
    you have to check in script if user provided username.... $#
    if [ $# -gt 0 ] ; then

    <=== { casper } ===>
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  6. #5
    Just Joined!
    Join Date
    Aug 2006
    Posts
    16
    I dont understand what my script is supposed to do.This is the whole question if someone could give any kind of advice at what i must do.

    write a script called "checking.sh" that will do:

    a)take a command line argument of a user's username
    b)test to make sure that a command line argument was provided. if not a usage message will appear on the screen and the script will exit.
    c) check to see if the user exists on the system.
    d) check to see if the user is logged on. If so, the program will print the processes that they are running.

  7. #6
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,729
    hi kristb17 !!!

    now dont tell me this is not your homework....
    you dont know anything about command line arguments...... and you are trying to write this kinda script...
    well ! you should study a few linux scripting tutorials first.....

    www.linuxcommnad.org
    http://dsl.org/cookbook/cookbook_toc.html
    http://www.tldp.org/LDP/abs/html/index.html

    i am not going to solve your problem..... read and study these tutorials... if you find anything difficult, we are here to help you ......

    <=== { casper } ===>
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  8. #7
    Just Joined!
    Join Date
    Aug 2006
    Posts
    16

    Scripting Help PLEASEEEEEEE!!!

    Hi there, this is my code which i have done. Im a novice so im sure there are plenty of reasons why it doesnt work. The name of my file is
    Code:
    rename.sh
    Please help

    thanks

    Code:
    #!/bin/sh
    clear
    echo "Enter filename"
    read filename
    if grep $filename rename.sh
    then
    echo "Filename exists"
    else
    clear
    fi
    
    echo "Enter the new name for the second file"
    read filename2
    if grep $filename2 rename.sh
    then
    echo "Filename exists"
    echo "Would you like to overwrite this file? y/n?"
    read verdict
    if[$verdict -eq"y"]
    echo filename >> filename2
    else
    if[verdict -eq"n"]
    echo "You have chosen not to overwrite the file. Goodbye."
    exit
    fi
    fi

  9. #8
    Just Joined!
    Join Date
    Aug 2006
    Posts
    16
    Since i dont understand it i will go through it step by step and tel me if im wrong.
    Thing is i am doing two question and both question are to do with arguments. so i need to do one
    then im sure i could do the other.

    Code:
    #!/bin/sh
    echo $1
    Right this shows the first argument ($1). Right? But when i run the script its just blank.

  10. #9
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,729
    hi kristb17 !!

    great ! .......
    $1 will show value if you pass an argument with script name...

    $ sh myscript hello

    here script name is myscript and you are passing an argument "hello"... now your code will display "hello"

    <=== { casper } ===>
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  11. #10
    Just Joined!
    Join Date
    Aug 2006
    Posts
    16
    Thats what i do not understand.

    My code so far only says

    Code:
    #!/bin/sh
    
    echo $1
    say my code in my script is
    Code:
    #!/bin/sh
    echo $1
    echo $2
    this shows two arguments. Right? My question is to take a command line argument of a users username. so, would i


    Code:
    #!/bin/sh
    echo "Enter username"
    read username
    echo "checking $1"
    echo $1
    echo $2
    ??

Page 1 of 4 1 2 3 4 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
  •