Results 1 to 8 of 8

Thread: Shell Scripting     submit to reddit submit to twitter

  1. #1
    Rainbow Dash was here,
    Applejack is a silly filly.

    Join Date
    Jan 2008
    Posts
    1,425
    BG Level
    6

    Shell Scripting

    Yes, this is homework. No, my teacher has not actually taught us scripting, he kind of just expected us to pick it up along the way since this is an accelerated course. I'm not expecting you to just give me the answer (But I'm not complaining if you do), but I am asking for help.

    The first problem is "Write a shell script that will add two numbers, which are supplied as command line arguments, and if this two numbers are not given, show an error message."

    I can do the first part of that
    Code:
    echo `expr $1 + $2`
    works just fine to add two numbers supplied as command line arguments, but now I need to figure out how to make it so that if there are no arguments given to return an error message.

    I was looking through the book and I was thinking that the ${VAR:-} thing would be useful as in.

    Code:
    $1=${1:- "Not Set"}
    if $1 = "Not Set" ; then
         echo "No arguments"
    else
         echo `expr $1 + $2`
    fi
    I'm more than positive I'm doing something wrong because I simply dont know the syntax and all the examples I'm getting off of online shell scripting crap is the stuff you do in by command line without writing an actual script where you put an $ before every line, well, fuck, honestly I just dont know what I'm doing.

  2. #2
    Rainbow Dash was here,
    Applejack is a silly filly.

    Join Date
    Jan 2008
    Posts
    1,425
    BG Level
    6

    I just realized I think I might be having a problem because I sort of remember my teacher saying "Dont forget to add [whatever] at the top of your script" at the end of class as he handed out this sheet of homework.

    I think it's the equivalent of adding #include <cstdlib> at the top of a C++ file, but in this it's supposed to be something like #./bash/ or something.

  3. #3
    Banned.

    Join Date
    Jul 2005
    Posts
    5,821
    BG Level
    8
    FFXI Server
    Sylph
    WoW Realm
    Arthas

    Try:

    Code:
    if [ -n "$1" ] || [ -n "$2" ]
    then
      echo `expr $1 + $2` 
    else  
      echo "OH SHIT SON!"
    fi

  4. #4
    Rainbow Dash was here,
    Applejack is a silly filly.

    Join Date
    Jan 2008
    Posts
    1,425
    BG Level
    6

    Thank you only person who cares about me.

  5. #5
    Rainbow Dash was here,
    Applejack is a silly filly.

    Join Date
    Jan 2008
    Posts
    1,425
    BG Level
    6

    So yeah, that works, and I can understand why it works, but I still need someone to tell me what I was doing so horribly wrong on my example, I know it's horribly wrong, I just dont know why.

  6. #6
    Rainbow Dash was here,
    Applejack is a silly filly.

    Join Date
    Jan 2008
    Posts
    1,425
    BG Level
    6

    So question 2, "Write a shell script to determine whether given file exists or not, the file name is supplied as command line argument. Also check for a sufficient number of command line arguments." I take it the last part just means to check if they give any arguments at all, 'cause you only really need one argument, right?

    Anyway here's the code plus what happens when executed:
    [img]removed, I see what's wrong for now[img]

  7. #7
    Relic Weapons
    Join Date
    Oct 2006
    Posts
    335
    BG Level
    4

    Quote Originally Posted by #686578 View Post
    So yeah, that works, and I can understand why it works, but I still need someone to tell me what I was doing so horribly wrong on my example, I know it's horribly wrong, I just dont know why.
    The very first line in a shell script is the shell executable. If you're using bash, it will probably be:

    #!/bin/bash

    , but the path (/bin/bash) may vary with where you have the executable installed on your system. It's not the same as a including a library; with this line you are telling the shell what program to use to interpret the rest of your script.

    You can't make assignments to positional parameters in bash ($1, $2, etc.). As an exercise, you should see how that line really evaluates after the shell makes its regular substitutions (hint: use echo).

    Your example has the right idea (even though it doesn't work), but you're complicating it more than necessary. If you understand Senoska's example's syntax, you probably already understand how that's the case. If you want to make your example work properly, you need to:

    1. find the correct syntax for assigning values to variables
    2. find the correct syntax for test operators in conditionals
    3. remember that what you wrote may not be what the shell executes AFTER substitution
    4. make sure your error checking covers all of the situations you want it to (ie., Senoska's example is syntactically, but not functionally, correct).

    Deliberately vague because it's a homework assignment.

  8. #8
    BG's most likeable Québécois
    Pens win! Pens Win!!! PENS WIN!!!!!

    Join Date
    Sep 2007
    Posts
    37,887
    BG Level
    10

    Before
    if [ -n "$1" ] || [ -n "$2" ]
    then
    echo `expr $1 + $2`
    else
    echo "OH SHIT SON!"
    fi

    you can add read -p "Enter first number" 1
    read -p "Enter 2nd Number" 2

    So it will actually ask the question to the user so he doesnt forget to add the arguments

    Ps i forgot if it supports 1 and 2 like that for Arguments or only as inition

    so can rep exeple 1 by numb1 and 2 by numb2

    and in the script rep $1 by $numb1 and $2 by $numb2 your call

Similar Threads

  1. Shell scripting
    By #686578 in forum Tech
    Replies: 7
    Last Post: 2009-02-26, 19:52