Find the answer to your Linux question:
Page 2 of 4 FirstFirst 1 2 3 4 LastLast
Results 11 to 20 of 34
when you pass user's username it get stored in $1 #!/bin/sh echo "Enter username" read username if [ $username = $1 ] ; then echo " Correct " else echo ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #11
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,729

    when you pass user's username it get stored in $1

    #!/bin/sh
    echo "Enter username"
    read username
    if [ $username = $1 ] ; then
    echo " Correct "
    else
    echo "Wrong Username"
    fi
    copy and paste this code in file "check_user"

    $ sh check_user kristb17

    it will ask for username, if you enter "kristb17", it will give "correct" message

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

  2. #12
    Just Joined!
    Join Date
    Aug 2006
    Posts
    16

    Smile

    Thank you so much. I think i understand now. That what i mean. Ive been tutorials but i couldn find what i wanted. i just needed an example i can work around. Thanks for your help.

    thanks

    krist

    Sure ul be hearing from me again!!!

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

    you are welcome... we are here to help you... study well !!


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

  4. #14
    Just Joined!
    Join Date
    Aug 2006
    Posts
    16
    Sorry me again.

    This is what i have done. i want the user to input 2 arguments. 1st argument is the name of the original file, and second argument is new name for the file. if the name does already exist the script should prompt the user for a decision about whether the file should be overwritten.

    Code:
    #!/bin/sh
    clear
    echo "Enter name of original file"
    read filename
    if [ $filename = $1 ] ; then
    echo "The filename you entered is $filename"
    
    clear
    
    echo "Enter the new name for the file"
    read filename2
    if [ $filename2 = $2 ] ; then
    echo "The filename you entered is $filename2"
    
    echo "Do you want to overwrite the first filename? y/n?"
    read verdict
    if [ $verdict -eq "y" ] then
    echo filename2 >> filename
    else
    if [ $verdict -eq "n" ] then
    echo "You have chosen not to overwrite. The script will now exit"
    
    fi
    fi
    exit
    Sorry again

  5. #15
    Banned jan1024188's Avatar
    Join Date
    Jul 2006
    Location
    EU, SLOVENIA
    Posts
    1,549
    hi kristb17
    sory?why? you are not disturbing us......we are glad if we can help

    i have repaired your script

    here it is:
    Code:
    #!/bin/sh
    clear
    echo "Enter name of original file"
    read filename
    if [ $filename = $1 ]; then
    echo "The filename you entered is $filename"
    fi
    
    clear
    
    echo "Enter the new name for the file"
    read filename2
    if [ "$filename2" = "$2" ]; then
    echo "The filename you entered is $filename2"
    fi
    echo "Do you want to overwrite the first filename? (y/n)?"
    read verdict
    if [ "$verdict" = "y" ]; then
    echo "$filename2 >> $filename"
    elif [ "$verdict" = "n" ]; then
    echo "You have chosen not to overwrite. The script will now exit"
    exit 0
    fi
    i hope that helps

  6. #16
    Banned jan1024188's Avatar
    Join Date
    Jul 2006
    Location
    EU, SLOVENIA
    Posts
    1,549
    but that script wont work(not yet)
    if you want to "rename" files you have to use mv command

    by adding this line you get script which would work

    Code:
    mv $filename2 $filename
    you put it here
    Code:
    #!/bin/sh
    clear
    echo "Enter name of original file"
    read filename
    if [ $filename = $1 ]; then
    echo "The filename you entered is $filename"
    fi
    
    clear
    
    echo "Enter the new name for the file"
    read filename2
    if [ "$filename2" = "$2" ]; then
    echo "The filename you entered is $filename2"
    fi
    echo "Do you want to overwrite the first filename? (y/n)?"
    read verdict
    if [ "$verdict" = "y" ]; then
    echo "$filename2 >> $filename"
    mv $filename2 $filename    <------ mv command after mssage 
    elif [ "$verdict" = "n" ]; then
    echo "You have chosen not to overwrite. The script will now exit"
    exit 0
    fi

  7. #17
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,729
    if [ -f $2 ]; then
    echo "file " $2 " already exists !!! Overwrite ?? y|n"
    read verdict
    if [ $verdict = "y" ]; then
    mv $1 $2
    echo "New Name Assigned "
    else
    echo "overwriting cancelled. New Name not assigned."
    fi
    else
    mv $1 $2
    echo "New Name Assigned"
    fi
    $ sh script1 myfile1 myfile2

    a very simple script..... study it...

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

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

    scripting

    Hi there thanks again for your help. It is much appreciated. Can someone check this script for me? I think there are a few problems. Also the presentation of the script isnt very good when it runs. Is there anyway i can make the script look nicer when it runs?

    thanks again

    Code:
    #!/bin/bash
    clear
    echo "Please enter your firstname and lastname"
    read FN LN
    echo "Hi there $FN"
    
    echo "Enter your birth year. Example 1900 :"
    read year
    echo "Enter your birth month. Example: 06"
    read month
    yearnow=$(date '+%Y')
    monthnow=$(date '+%m')
    agey=$(expr $yearnow - $year)
    agem=$(expr $monthnow - $month)
    if [ $agem -lt 0 ] ; then
    agem=$(expr $monthnow - $month + 12)
    else
    agem=$(expr $monthnow - $month)
    fi
    echo "You are $agey years and $agem months old!"
    
    echo "Please enter a username"
    read username
    
    passwdline= 'cat/etc/passwd | grep $username'
    echo $HOME /etc/passwd
    
    ps -ef | grep $FN
    
    date +"The day of the week is %A, and the current time is"
    date | cut -c11-
    
    ps -U root -u root u
    
    who | cut -c1-9,36-
    
    echo $PATH
    echo $HOME
    echo $TERM
    
    df -h
    
    echo "Goodbye $FN $username"
    date | cut -c11-
    
    exit
    
    clear

  9. #19
    Just Joined!
    Join Date
    Aug 2006
    Posts
    16

    scripting

    Hi there im still trying to do the previous question. I will write out the whole question and then tell me what i have missing.Please help. thanks

    Write a script called "rename.sh" that will take two arguments: the first is the name of the original file and the second is the new name for the file. If a user doesn provide two arguments a message will appear and the script will exit. The script should check to see if the name already exists. If the name already exists the script should prompt the user for a decision about overwritting the file.

    So:
    Write a script called "rename.sh" that will take two arguments: the first is the name of the original file and the second is the new name for the file.
    [CODE]
    #!/bin/sh
    echo $1
    echo $2
    echo "The first filename you entered was $1"
    echo "The new name you have chosen for the file is $2"

    Code:
    If a user doesn provide two arguments a message will appear and the script will exit.
    Dont know how to do this

    The script should check to see if the name already exists.
    So:
    Code:
    if [ "$2" = "$1" ] ; then
    echo "Do you want to overwrite the first filename? y/n"
    read verdict
    fi
    if [ "$verdict" = "y" ] ; then
    echo "$2 >> $1"
    mv "$2 $1"
    elif [ "$verdict" = "n" ] ; then
    echo "You have chosen not to overwrite. The script will now exit."
    exit 0
    fi

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

    are you scripting these yourself ? good !!
    there is nothing wrong in this script except line 25 ..

    passwdline= 'cat /etc/passwd | grep $username'
    you missed "space" after 'cat'....
    for good presentation, use 'echo' .... ********** or ======= signs after commands....

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

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