Find the answer to your Linux question:
Results 1 to 6 of 6
Why doesn't my bash script like my if statement? What I got so far is echo -n "Enter Filename:" s=0 read fn if (find $fn); then while read k do ...
  1. #1
    Just Joined!
    Join Date
    Aug 2010
    Posts
    6

    help with if statement bash

    Why doesn't my bash script like my if statement?

    What I got so far is

    echo -n "Enter Filename:"
    s=0
    read fn
    if (find $fn); then
    while read k
    do
    echo $k
    let $n+$k
    done <---- might be wrong $fn
    echo " "
    echo " "
    echo "Sum is" $n
    echo " "
    read -p "Enter to continue"
    done
    fi

    can someone help me out?

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    what are you trying to do?
    the sun is new every day (heraclitus)

  3. #3
    Linux Enthusiast
    Join Date
    Aug 2006
    Location
    Portsmouth, UK
    Posts
    539
    Bash uses square brackets in if statemetnts []

    Take a look at the Bash Introduction to if
    RHCE #100-015-395
    Please don't PM me with questions as no reply may offend, that's what the forums are for.

  4. #4
    Just Joined!
    Join Date
    Aug 2010
    Posts
    6
    I'm trying to get numbers from a file and for the script to read the numbers could be any number and any amount of numbers and for the script to add the numbers up in the end

  5. #5
    Just Joined!
    Join Date
    Aug 2010
    Posts
    6
    [QUOTE=matonb;797751]Bash uses square brackets in if statemetnts []



    Maybe that's what's wrong but another person used () and it worked fine for me so I don't know.

  6. #6
    Just Joined!
    Join Date
    Jul 2006
    Posts
    6
    Try this one:
    Code:
    #!/bin/bash
    
    SUM=0
    
    while read -p 'Enter filname: ' FN; do
    	if [[ -f "$FN" ]]; then
    		N=$(<"$FN")
    		(( SUM += N ))
    		echo "Current sum is $SUM."
    		read -p "Press enter key to continue."
    	else
    		echo "Entered file does not exist: $FN."
    	fi
    done
    
    # CTRL-C = abort

Posting Permissions

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