Nested if question BASH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nested if question BASH
# 1  
Old 01-23-2011
Nested if question BASH

Just started learning bash ,and I am confused with sintaksis

line 16: syntax error near unexpected token `else'

thanks
Code:
 
#!/bin/bash
echo -n "Enter: "
read num
 if(($(echo ${#num}) == 0 ))
   then
    echo No arguments passed.Try again
 elif
    rem=$(echo $num | tr -d [0-9])
    lenght=$(echo ${#rem})
      if(( $lenght > 0))
        then
       echo "$num is not a number"
      else
       echo "$num is a number"
       fi
  else
    echo "Good job"      
    fi

# 2  
Old 01-23-2011
Hi
The elif does not have any associated condition and hence the error.

Guru
# 3  
Old 01-23-2011
Hi,
Code:
#!/bin/bash

echo -n "Enter: "
read num

if(( ${#num}) == 0 ))
then
   echo "No arguments passed.Try again"
#elif #elif what? should be else
else
   if(( $(echo "$num" | tr -d [0-9]) > 0))
   then
      echo "$num is not a number"
   else
      echo "$num is a number"
   fi
#else #shouln't be here
   echo "Good job"
fi

# 4  
Old 01-23-2011
Thanks a lot
# 5  
Old 01-23-2011
Often we are testing input and if is not good, then exit is also nice solution, no need to write else/elseif.
Code:
#!/usr/bin/bash or ksh or ...
echo -n "Enter: "
read num

[ "$num" = "" ] && "No arguments passed.Try again" && exit 1

# replace numbers using nothing
other=${num//[0-9]/} # remove numbers using builtin replace = like tr -d "[0-9]"

# if something left, those are not numers
[ "$other" != "" ] && echo "$num is not a number" && exit 2

echo "Good job"

# 6  
Old 01-24-2011
Hi again ,and thanks for the answers
how can I pass arguments to the same function
Code:
 
#!/bin/bash
echo -n "Enter: "
read num
check($num)
function check($num)
{
 if(($(echo ${#num}) == 0 ))
   then
    echo No arguments passed.Try again
 else
    rem=$(echo $num | tr -d [0-9])
    lenght=$(echo ${#rem})
      if(( $lenght > 0))
        then
       echo "$num is not a number"
      else
       echo "$num is a number"
       fi
 fi
}

# 7  
Old 01-24-2011
Quote:
Functions may process arguments passed to them and return an exit status to the script for further processing.

function_name $arg1 $arg2


The function refers to the passed arguments by position (as if they were positional parameters), that is, $1,$2, and so forth.
Another note:-

Quote:
The function definition must precede the first call to it. There is no method of "declaring" the function, as, for
example, in C.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Nested loop -bash

I am using the following nested loop for i in {1..3} do for y in {1..3} do if ; then echo P0${i}R${y}.fas mv P0${i}R${y}.fas P${i}R${y}.fas read -t 5 fi done done I was wondering if I can use a character such as * or ? instead of my second variable y. I tried R in... (3 Replies)
Discussion started by: Xterra
3 Replies

2. Shell Programming and Scripting

Nested double quotes won't work in my bash script?

In a bash script I have: LSCMD="find /project/media/ -mindepth 2 -maxdepth 2 -name \"files*pkg\"" ALL_PACKAGES=$( $LSCMD | sort 2>/dev/null) But I get nothing returned. It's just all blank. If I run the find command in a terminal, I get dozens of hits. I figure it's the way how I'm... (3 Replies)
Discussion started by: superbbrr
3 Replies

3. Shell Programming and Scripting

Help with nested $s and quotations in bash / awk

Folks - newbie bash coder here and I'd like to get your help to make the code below work. As you can see, I was trying to count the total number of lines with the 3rd value >= 15 in a file and wanted to make the threshold "15" configurable, but apparently the $THRESHOLD value was not populated... (3 Replies)
Discussion started by: bashzipper
3 Replies

4. Shell Programming and Scripting

Complex bash/sed, variables and nested quotes

Ok, this one isn't for everybody, it's pretty tough and I've spent a good deal of time on it without figuring it out yet. Can anybody get this script to work: #!/bin/bash cq_fname="%let outputfile="/user/cq_"$1".csv";" sed "29s/.*/\"$cq_fname\"/" file1.sas >... (3 Replies)
Discussion started by: nocloud
3 Replies

5. Shell Programming and Scripting

nested logical expression in bash shell

Please tell me how to nest logical expressions in bash. I would like to nest logical expressions for arguments of the "test" command on bash. The following pseudo-code shows my intention. // pseudo code if (exp1 AND (exp2 OR exp3)) { Output true; } else { Output false; } ... (11 Replies)
Discussion started by: LessNux
11 Replies

6. Shell Programming and Scripting

Nested for loop in bash

Hi, How to use values in one for loop to other for loop. say "$sf_rel" variable has values "2011/W2 2011/G2" I want to use these values in inner for loop to process properly. $branch variable has G2 and 6 What is happening is outer for loop $i has 2011/W2 it is entering into inner... (3 Replies)
Discussion started by: Anjan1
3 Replies

7. UNIX for Dummies Questions & Answers

Bourne-sh (not bash) question about nested loops and sed

Here's the input: alpha, numeric or alphanumeric string ("line 1 string") numeric string ("line 2 string") numeric string ("line 3 string") numeric string ("line 4 string") ... where - each numeric string is in a pattern that can be matched with RE but - there can be any number of... (2 Replies)
Discussion started by: uiop44
2 Replies

8. Shell Programming and Scripting

Bash: Nested functions and including other scripts

Hello. I have the following problem with bash code: function fl1_load_modules_and_get_list() ........... for module in $FL_MODULES_TO_PROCESS do source "${FL_MODULE_DIR}/${module}/module.sh" done ........... } function fl1_handle_install { local... (12 Replies)
Discussion started by: FractalizeR
12 Replies

9. UNIX for Dummies Questions & Answers

Nested If question

if ]; then if ]; then rm -f ${LOGFILE}.old fi mv ${LOGFILE} ${LOGFILE}.old fi Havent done nested ifs in a while. I'm reading someones code If I'm reading this correctly. It checks for the logfile, and if it exists it checks for the old logfile and if that exists, it removes the... (8 Replies)
Discussion started by: NycUnxer
8 Replies

10. Shell Programming and Scripting

nested looping question

Hi, I'm having some trouble with the syntax in constructing a simple nested 'for' loop. My code is as follows: #!/bin/bash dir1="fred flume haystack" for dir2 in ${dir1} do fred="1 2 3" flume="a b c" ... (7 Replies)
Discussion started by: Sn33R
7 Replies
Login or Register to Ask a Question