Results 1 to 5 of 5

Thread: Bash script help

  1. #1
    Join Date
    Jun 2006
    Beans
    151
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Bash script help

    Hey all. I'm trying to write a small bash script and not having any luck getting it to work properly. I was hoping that someone here could take a look and tell me where I'm going wrong. The script is supposed to pull an archive file, replace all the dates in the file with today's date and tomorrow's date, spit out a new file, and then load it into Oracle. Here's what I've got:

    Code:
    #script tester.sh
    #!/bin/bash
    #Define variables
    todaysDate=$(date +%Y%m%d)
    tomorrowDate=$(date --date=tomorrow +%Y%m%d)
    path=/home/scripts
    cd $path
    rm skedbase.txt
    sed -e "s/20080729/$todaysDate/g" -e "s/20080730/tomorrowDate/g" skedbase.txt.old > skedbase.txt
    su -l oracle -c "cd /oracle/scripts; ./runLoader.sh &"
    exit 0
    So I'm hitting two errors I don't understand. First :
    Code:
    cd: 7: can't cd to /home/scripts
    It doesn't say why it can't change directories.

    The second problem is that it performs the sed operation correctly, but it spits out a file named 'skedbase.txt?' instead of 'skedbase.txt'. I can't figure out why its doing that or how to fix it.

    Suggestions?
    Last edited by theacoustician; October 21st, 2008 at 06:17 PM.

  2. #2
    Join Date
    Mar 2008
    Beans
    1,755

    Re: Bash script help

    You should not us cd in a script. You should always use absolute paths. Example:

    Instead of:

    path=/home/scripts
    cd $path
    rm skedbase.txt

    Do:

    rm /home/scripts/skedbase.txt

  3. #3
    Join Date
    Jun 2006
    Beans
    151
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Bash script help

    Quote Originally Posted by Titan8990 View Post
    You should not us cd in a script. You should always use absolute paths. Example:

    Instead of:

    path=/home/scripts
    cd $path
    rm skedbase.txt

    Do:

    rm /home/scripts/skedbase.txt
    Ok, good to know. Any idea on why I keep getting a ? added to the end of the sed output file?

  4. #4
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Bash script help

    Firstly, make sure the sh-bang (#!/bin/bash) is the first line of the script.

    The problem with the filename sounds like you've edited the file in windows, which uses \r\n instead of \n for a new line. So the ? in the filename probably represents a \r.

    Use sed to remove \r from the end of the lines
    Code:
    sed -i 's/\r$//' script.bash
    EDIT: Oh and to spot these things in the future try
    Code:
    cat -v script.bash
    which will represent '\r's as ^M.
    Last edited by geirha; October 21st, 2008 at 06:35 PM.

  5. #5
    Join Date
    Jun 2006
    Beans
    151
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Bash script help

    Quote Originally Posted by geirha View Post
    Firstly, make sure the sh-bang (#!/bin/bash) is the first line of the script.

    The problem with the filename sounds like you've edited the file in windows, which uses \r\n instead of \n for a new line. So the ? in the filename probably represents a \r.

    Use sed to remove \r from the end of the lines
    Code:
    sed -i 's/\r$//' script.bash
    EDIT: Oh and to spot these things in the future try
    Code:
    cat -v script.bash
    which will represent '\r's as ^M.
    Just wanted to update and say this worked. Thanks!

Bookmarks

Posting Permissions

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