Find the answer to your Linux question:
Results 1 to 3 of 3
Code: #!/bin/bash echo $1 " before if $ 1 " function calc_wtime() { echo "$1 pram 1 hello" ## if pram 1 is NULL or not entered or 1...3 is ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2013
    Posts
    20

    these [ ] .. [[ ]] have me more confused then a cat in a dog fight


    Code:
    #!/bin/bash
    
    
    echo $1 " before if $  1  "
    
    function calc_wtime()
    {
        echo "$1 pram 1  hello"
    
    ## if pram 1 is NULL or not entered or 1...3 is entered then it defualts to 3 minutes
    
    #if [ $1 = " " ] || [ $1 <= 3 ] ;
    
    #if [[ $1 = " " || $1 <= 3 ]]
    
    #if [[ $1 == " " || $1 <= 3 ]]
    
    if [[ $1 == " " || $1 <= "3" ]]
    
               then
               
               
               #            condition is zero (true - 0)
               #            execute all commands up to else statement
               
               t=( 3 * 60 )
    
               else
                           #if condition is not true then
                           #execute all commands up to fi
                           
               t=($1 * 60)
               
               fi
    
    return $t
    }
    
    calc_wtime
    gave me said errors
    Code:
    userx bw-crunchbang:~$ ./hitme 4
    byte me
    4  before if $  1  
     pram 1  hello
    ./hitme: line 17: [: =: unary operator expected
    ./hitme: line 17: =: No such file or directory
    userx bw-crunchbang:~$ ./hitme 4
    byte me
    4  before if $  1  
     pram 1  hello
    ./hitme: line 17: [: =: unary operator expected
    ./hitme: line 17: =: No such file or directory
    userx bw-crunchbang:~$ 
    userx bw-crunchbang:~$ ./hitme 4
    4  before if $  1  
     pram 1  hello
    ./hitme: line 16: [: missing `]'
    ./hitme: line 16: =: No such file or directory
    userx bw-crunchbang:~$ ./hitme 4
    4  before if $  1  
    ./hitme: line 16: syntax error in conditional expression
    ./hitme: line 16: syntax error near `3'
    ./hitme: line 16: `if [[ $1 = " " || $1 <= 3 ]]'
    userx bw-crunchbang:~$ ./hitme 4
    4  before if $  1  
    ./hitme: line 18: syntax error in conditional expression
    ./hitme: line 18: syntax error near `3'
    ./hitme: line 18: `if [[ $1 == " " || $1 <= 3 ]]'
    userx bw-crunchbang:~$ ./hitme 4
    4  before if $  1  
    ./hitme: line 20: syntax error in conditional expression
    ./hitme: line 20: syntax error near `"3"'
    ./hitme: line 20: `if [[ $1 == " " || $1 <= "3" ]]'
    userx bw-crunchbang:~$


    if pram is null or pram is less then or equal to 3 then multiply it by 60 else take what ever number pram 1 is and multiply it by 60 return result -- why is it so hard to wite that in bash? in C++ if memory serves me right it is --

    Code:
    //if no pram $1 then it could be written for this funt call
    
    int WhateverFuntionName(){
    
    return(3*60);
    }
    
    
    int WhateverFuntionName(int pram1){
    
    //if a var is passed then this one would be called
    
    int b
    
    if (b == ""  || b <= 3) then
    
    return (3 * 60);
    else
    return (b * 60);
    
    }
    with it written like this

    Code:
    
    if [ $1 <= 3 ]
               then
               ....
    it gives me this error

    Code:
    passing this into the commande line --  ./hitme 4
    I get this
    
    4  before if $  1  
     pram 1  hello
    ./hitme: line 20: =: No such file or directory
    all of this [ ] [[ ]] "" and what not is confusing me ?? can anyone explane without questions ??

    it is a simple funtion if a number greater then 3 is passed into the first pram $1 when calling it on the command line then times it by 60 and pass it back out to the prog or script using it to change the var already set at startup


    thanks

  2. #2
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,622
    I thought or in bash was expressed like this
    Code:
    if [ $1 == " " -o $1 <= "3" ]
    and and like
    Code:
    if [ $1 == " " -a $1 <= "3" ]
    "I used to be with it, then they changed what it was.
    Now what was it isn't it, and what is it is weird and scary to me.
    It'll happen to you too."

    Grandpa Simpson



    The Fifth Continent

  3. #3
    Just Joined!
    Join Date
    Nov 2013
    Posts
    20
    Quote Originally Posted by elija View Post
    I thought or in bash was expressed like this
    Code:
    if [ $1 == " " -o $1 <= "3" ]
    and and like
    Code:
    if [ $1 == " " -a $1 <= "3" ]
    I figured it out even thugh I don't fully understand it

    Code:
    #!/bin/bash
    
    
    Stime=3
    newT="$1"
    
    
    if [[ $newT -le $Stime ]] ; then
    
      
    
      ans=$((3*60))
      echo "first condistion is " $ans
      
     
    else
       ans=$(($newT*60))
       
       echo "second condistion is " $ans
      
    fi
    if one enters any number greater then 3 it then cals the second condistion for the command line argument being $1

  4. $spacer_open
    $spacer_close

Posting Permissions

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