Find the answer to your Linux question:
Page 3 of 4 FirstFirst 1 2 3 4 LastLast
Results 21 to 30 of 34
If a user doesn provide two arguments a message will appear and the script will exit. if [ $# -lt 2 ] ; then echo "New File Name Missing" exit ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #21
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,729

    If a user doesn provide two arguments a message will appear and the script will exit.
    if [ $# -lt 2 ] ; then
    echo "New File Name Missing"
    exit 0
    fi

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

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

    scripting

    Hi there can someone check why this code doesn work please?

    thanks again!!

    Code:
    #!/bin/sh
    
    echo "First Argument"
    echo $1
    
    echo "Second Argument"
    echo $2
    
    if [ $# -lt 2 ] ; then
    echo "New File Name Missing"
    fi
    
    if [ -f $2 ] ; then
    echo "file" "$2" alread exists!! Overwrite y/n?"
    read verdict
    fi
    
    if [ $verdict = "y" ] ; then
    mv $1 $2
    echo "New Name Assigned"
    elif [ $verdict = "n" ] ; then
    echo "Overwriting cancelled. New name not assigned.
    fi
    exit 0

  3. #23
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,729
    if [ -f $2 ] ; then
    echo "file" "$2" alread exists!! Overwrite y/n?"
    read verdict
    fi
    read verdict is in if construct... if condition false, where is value of verdict ???

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

  4. #24
    Just Joined!
    Join Date
    Aug 2006
    Posts
    16

    scripting

    i dont understand what you mean? i closed the if statement by putting the fi first then read verdict. Thats exactly where the problem starts on my script because its all red in my code.

  5. #25
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,729
    if [ -f $2 ] ; then
    echo "file" "$2" alread exists!! Overwrite y/n?"
    read verdict
    fi
    in this section, you are reading the value of "verdict".
    but if condition is false, then "read verdict" statement wont execute.... so.. how can you check its value in section below........

    if [ $verdict = "y" ] ; then
    mv $1 $2
    echo "New Name Assigned"
    elif [ $verdict = "n" ] ; then
    echo "Overwriting cancelled. New name not assigned.
    fi
    regarding colors, change colors.....
    Session --> configure Konsole ---> schema

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

  6. #26
    Banned jan1024188's Avatar
    Join Date
    Jul 2006
    Location
    EU, SLOVENIA
    Posts
    1,549
    hi kristb17
    is that script for practice or work?
    what should it do?

    echo "First Argument"
    echo $1

    echo "Second Argument"
    echo $2

    i would add read line

    echo -n "First Argument >"
    read $1
    echo $1

    echo -n "Second Argument >"
    read $2
    echo $2

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

    scripting

    Well both really. This is what i want it to do:

    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.

    This is what i have done:

    Code:
    echo "First Argument"
    echo $1
    
    echo "Second Argument"
    echo $2
    
    if [ $# -lt 2 ] ; then
    echo "New File Name Missing"
    fi
    
    if [ -f $2 ] ; then
    echo "file" "$2" alread exists!! Overwrite y/n?"
    read verdict
    fi
    
    if [ $verdict = "y" ] ; then
    mv $1 $2
    echo "New Name Assigned"
    elif [ $verdict = "n" ] ; then
    echo "Overwriting cancelled. New name not assigned.
    fi
    exit 0

  8. #28
    Banned jan1024188's Avatar
    Join Date
    Jul 2006
    Location
    EU, SLOVENIA
    Posts
    1,549
    huh lot of mistakes....i repaired them..... that should work

    Code:
    #!/bin/bash
    
    echo "First Argument"
    read $1
    echo "$1"
    
    echo "Second Argument"
    read $2
    echo "$2"
    
    if [ "$#" = "$2" ]; then
    echo "New File Name Missing"
    fi
    
    if [ "$1" = "$2" ]; then
    echo "file $2 alread exists!! Overwrite (y/n)?"
    read verdict
    fi
    
    if [ "$verdict" = "y" ]; then
    mv $1 $2
    echo "New Name Assigned"
    elif [ "$verdict" = "n" ]; then
    echo "Overwriting cancelled. New name not assigned."
    exit 0
    fi
    just i dont know what to do here with this line
    Code:
    if [ "$#" = "$2" ]; then
    echo "New File Name Missing"
    fi
    so i putted in =...that doesnt make troubles

    i hope that helps

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

    scripting

    Thanks again for all of your help. Thanks.
    I previously wrote a code here.

    I was wondering if anyone could tell me how to ask the users username and print his/her home directory path.

    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

  10. #30
    Banned jan1024188's Avatar
    Join Date
    Jul 2006
    Location
    EU, SLOVENIA
    Posts
    1,549
    the script is ok...just i dont know why:
    echo "Goodbye $FN $username" <---- twice the same thing
    date | cut -c11-

    thats better
    echo "Goodbye $username"
    date | cut -c11-

    ah and here
    Code:
    cat/etc/passwd | grep $username
    that wont work
    Code:
    cat /etc/passwd | grep $username
    thats better but still wont work
    check
    Code:
    man grep
    when you have a word with special characters and you want to use grep...quotas are solution
    Code:
    cat /etc/passwd | grep "$username"

    anyway what do you mean with
    print his/her home directory path.

Page 3 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
  •