Shell scripts

Discussion in 'OT Technology' started by zanyspy_dude, Mar 9, 2006.

  1. zanyspy_dude

    zanyspy_dude King of teh n00bz

    Joined:
    Aug 29, 2002
    Messages:
    4,473
    Likes Received:
    0
    Location:
    Indianapolis, IN
    I need to write a linux shell script (for gentoo and ubuntu, but that's shouldn't matter). How do i use the diff command in an if test?

    basically i need this

    if [ diff file1 file 2 = "" ] then
    echo "no difference"
    else
    echo diff file1 file2
    fi

    problems: how do i get the if test to work, how do i echo out a command, not text?
     
  2. crontab

    crontab (uid = 0)

    Joined:
    Nov 14, 2000
    Messages:
    23,439
    Likes Received:
    11
    this homework or for work

    and what shell is this for, i'm assuming bourne again.
     
  3. peerk

    peerk New Member

    Joined:
    Mar 14, 2005
    Messages:
    984
    Likes Received:
    0
    Here is how I would do it. But I'm sure there is a better way. I don't do shell scripting much.

    diff $1 $2 > out
    if test $? -eq 0
    then
    echo "no difference"
    else
    cat out
    fi
     
  4. zanyspy_dude

    zanyspy_dude King of teh n00bz

    Joined:
    Aug 29, 2002
    Messages:
    4,473
    Likes Received:
    0
    Location:
    Indianapolis, IN
    well, i'm simplifiying my homework. I have to type diff in the console (bourne) but i was going to put this in my make file to make my life easier.

    Thanks peerk...


    do you mean test out -eq 0 though? What is $?
     
  5. crontab

    crontab (uid = 0)

    Joined:
    Nov 14, 2000
    Messages:
    23,439
    Likes Received:
    11
    That's good then.

    With your style of syntax, the correct syntax is:

    if [ "`diff /path/file1 /path/file2`" = "" ]
    then
    echo "no difference"
    else
    diff /path/file1 /path/file2
    fi

    You don't have to echo the output of the command in the else statement, you just run it again and it will output to stdout. If you have to echo it, you can replace it with "echo `diff /path/file1 /path/file2`" Use ticks to execute the command within the ticks. But this would put the output in a single line since it's a "variable" and all end of lines are ignored.

    You need the $ as a prefix to variable. For example;

    export THIS_VARIABLE=uNF
    echo "THIS_VARIABLE"
    echo "$THIS_VARIABLE"

    You will see the difference. The first echo, will literally echo "THIS_VARIABLE". The second echo statement will echo, "uNF"

    [[email protected] root]# export THIS_VARIABLE=uNF
    [[email protected] root]# echo "THIS_VARIABLE"
    THIS_VARIABLE
    [[email protected] root]# echo "$THIS_VARIABLE"
    uNF

    What "$?" explicitly shows is the exit status of the previous command.

    For diff: An exit status of 0 means no differences were found, 1 means some differences were found, and 2 means trouble.

    peerk's if statement checks the exit status of the "diff $1 $2 > out" command. If there are no differences, exit status of 0, it will echo "no difference", else print out the file "out" as stdout.
     
  6. zanyspy_dude

    zanyspy_dude King of teh n00bz

    Joined:
    Aug 29, 2002
    Messages:
    4,473
    Likes Received:
    0
    Location:
    Indianapolis, IN

    You are why i LOVE OT!! thanks for your amazing answer, got my make file all worked out :big grin:
     

Share This Page