[Solved] Bash scripting and sed substitution

Hello!
I am writing a script in order to substitute strings from one array to another one in texts.
For only one case it is working as the following :
sed '/ā/s/\(.*\)ā\(.*\)/\1a\21/g' temp.txt > temp2.txt
which converts ā in a word by the same word with a normal "a" and the number 1 at the end of the word (māng > mang1)
For many cases i've made some arrays and containing the rules in a srcipt file :
# These are the 4 databases containing the strings that are suposed to be replaced
data1[1]=ā
data1[2]=ē
data1[3]=ī
data1[4]=ō
data1[5]=ū
data1[6]=ǖ
data2[7]=á
data2[8]=é
data2[9]=í
data2[10]=ó
data2[11]=ú
data2[12]=ǘ
data3[13]=ǎ
data3[14]=ě
data3[15]=ǐ
data3[16]=ǒ
data3[17]=ǔ
data3[18]=ǚ
data4[19]=à
data4[20]=è
data4[21]=ì
data4[22]=ò
data4[23]=ù
data4[24]=ǜ
# This is the data base of output correspondances
data[1]=a
data[2]=e
data[3]=i
data[4]=o
data[5]=u
data[6]=ü
count=1
for base in {1..4} # For each database
do
for case in {1..6} # For each case
do
sed "/${data${base}[$count]}/s/\(.*\)${data${base}[$count]}\(.*\)/\1${data[$case]}\2$base/g" temp.txt > temp2.txt
let "count+=1" #go to the next case in the database
cat temp2.txt > temp.txt
done
done
I have a substitution issue in the sed line. In fact I am trying to make a double substitution and it doesn't works.
Like the first substitution ${data${base}[$count]} make 3 substitutions at a time… but I can't make it to work.
In that case it would give me, for instance, the string contained in data2[3].
I hope you understand what i mean. And i'd like to know how to deal with that substitution issue if you have an idea…
Last edited by jiehong (2010-09-26 07:49:25)

I've implemented what Procyon told in the part 2 and it's working with a small adaptation, which is great!!
I've just an issue now because the number will go right after a word but at the end of the ligne… even if words are spaced by a space… like :
hǎo
hào
wō wó wǒ wò wo
become :
hao3
hao4
wo wo wo wo wo1234
my sed ligne is now :
sed "/$(eval echo \$\{data$base[$count]\})/s/\(.*\)$(eval echo \$\{data$base[$count]\})\(.*\)/\1${data[$case]}\2$base/g" temp.txt > temp2.txt
Last edited by jiehong (2010-09-25 20:27:57)

Similar Messages

  • [solved] bash: difference `` and $()?

    Hey,
    i'm new to bash scripting but I'm using both of the syntaxes `` and $() quite often and they seem to do the same thing.
    I believe `` just evaluates and with $() i can do an array of evaluating sequences but i'm not sure and i didn't want to dig in too deep into some bash guides right now.
    Can you tell me where the difference is?
    Last edited by demian (2010-05-14 13:16:52)

    Trent wrote:I don't think $() is portable outside of bash.  That's what I've heard, anyway.
    $() is adopted and recommended by POSIX, so protability should not be a problem.
    Search for command substitution in google and see by yourself
    Last edited by eirika (2010-05-14 20:24:57)

  • Double-clicking a Bash script and having it execute in it's directory

    Hi. I'm having some trouble with a Bash script I've been working on.
    The first command in the script is to copy all of the contents of the directory it's located in (including itself) to another folder.
    The Bash command I'm using is:
    cp -R . "/Some/Other/Folder/"
    This works fine when I run the program from the Terminal. E.g.: ./testscript
    But, I need this script to be double clickable in the Finder. I've associated it with Terminal, but when I double-click on it, instead of copying the files from the script's local folder, it starts recursively copying files from the home folder!
    I think this is because when I double-click on the script, it starts a new Terminal window at the home folder, and the dot portion of "cp ." now refers to the home folder, instead of the directory containing the script.
    Can anyone help me figure out how to be able to double click on the script to run it, yet have the script start in it's original folder?
    Thanks for any help.
    -Bassam
    Macbook Pro   Mac OS X (10.4.6)   Recently switched from Windows XP

    Thanks Steve, that worked perfectly.
    I did have to add a / to "$myFolder/" in the second line, so that the contents of the folder was copied, not the folder itself.
    Thanks!

  • [SOLVED]bash script

    I had created a bash script which ensures that each of the applications has one instance. The problem is no applications are executed during startup. Here is my script:
    if [ -z "ps aux | grep wmCalClock | head -n -1" ]
    then
    wmCalClock -b 100 -arial -tc cyan -bc black -e xterm &
    fi
    if [ -z "ps aux | grep wmfire | head -n -1" ]
    then
    wmfire -L1 -B1 -s0 -C2 -P fireload_temp &
    fi
    if [ -z "ps aux | grep wmcpuload | head -n -1" ]
    then
    wmcpuload -lc red -a 95 &
    fi
    if [ -z "ps aux | grep wmmemload | head -n -1" ]
    then
    wmmemload -lc red -am 95 &
    fi
    Last edited by heyya (2009-12-25 04:19:33)

    Well, obviously not, as the string "ps aux | grep wmCalClock | head -n -1" is
    never empty What you probably wanted to write is
    "$(ps aux | grep wmCalClock | head -n -1)"
    This will put the output of the script into the string, rather than taking the
    command string itselft.

  • [SOLVED] Bash scripts to mount & unmount optical drive in Worker?

    I'm running XFCE on Arch with the HAL daemon being called in /etc/rc.conf.
    I can access media on my optical drive (DVD's or CD's) through the desktop icon that appears after HAL has recognised the drive, VLC automatically does its thing as does NeroLinux.
    The reason I'm posting is that I found a great DOpus clone yesterday called Worker - http://www.boomerangsworld.de/cms/worker/index?lang=en, which I am in the process of configuring.
    A problem I have is being able to access the optical drive via Worker.
    The way it is on my system, with HAL handling it, the first line (see below) appears after HAL mounts the media, which basically makes the two lines below it useless:
    /media/<title of disk>
    /media/cd
    /media/dvd
    I have tried configuring Worker to use /media/dvd (or cd), to access the optical media, these don't work for the reason stated above, & /dev/sd0 doesn't work either.
    So, do I have to turn off HAL, uncomment the lines in fstab & use mount?
    A little bash script, that would do the job for me would be great, as Worker will accept a script or a command string.
    I am a bash baby, so if someone can see a solution please post it?
    All input welcome.
    Thanks.
    Last edited by handy (2008-11-19 04:11:02)

    Zariel wrote:
    i guess something like this?
    %optical ALL=(ALL) NOPASSWD: ALL
    I found the clues for this in the sudoers manual:
    handy   ALL = NOPASSWD: /sbin/umount /CDROM,\
                    /sbin/mount -o nosuid\,nodev /dev/cd0a /CDROM
    Which works in so far as now mounting no longer needs the password.
    Which leaves me with the problem of trying to understand how to get Worker to mount the optical drive on command.
    If I enter the bash command in the Terminal as follows:
    mount /mnt/dvd
    the media is mounted, after which I can push the button in Worker, which I have configured with:
    /mnt/dvd
    & the root list of the optical media is displayed in the active panel of Worker.
    I just haven't been able to get Worker to use "mount /mnt/dvd" yet, there will be a way, I wonder how long it will take me to find it? lol
    Last edited by handy (2008-11-19 06:48:09)

  • (SOLVED) bash script header problem

    When i run this piece of code from the command line it works properly, but when i try and run it from a script it doesn't.
    It's supposed to be the beginning of a header
    printf "%*s\n" "$((COLUMNS))" " " | tr " " "=" ; printf "%*s\n" $((COLUMNS/2)) " Database " ; printf "%*s\n" $((COLUMNS)) " " | tr " " "="
    Any help would be appreciated, thanks
    Last edited by unilx (2012-04-23 23:58:53)

    COLUMNS is not defined in a normal script.
    Now to find a solution ...
    Edit: does "sourcing" the script work from an interactive session?
    e.g.
    ~$ ./test.sh
    ./test.sh: line 2: 0: command not found
    $ . test.sh
    bash: 89: command not found
    where test.sh has only
    #!/bin/bash
    $((COLUMNS))
    Edit2:
    try
    WIDTH=`stty size | cut -d" " -f2`
    then use $WIDTH instead of $((COLUMNS))
    Last edited by Trilby (2012-04-23 23:16:22)

  • [Solved] - Bash scripting - variable question.

    Hi!
    I have a little problem, I want to get data from a variable, but the variable-name that I want to
    get the data from is constructed by another variable.
    Let me explain with a little non-working example:
    test1="data1"
    test2="data2"
    test3="data3"
    for i in {1..3};do
    echo value=$[test$i];done
    I want to get the output of this code to be:
    value=data1
    value=data2
    value=data3
    and I cannot use arrays in the script I'm working on. I know how to do this in other languages
    but cannot figure it out how to do it in bash.
    Please help!
    Last edited by JSHN (2009-10-06 19:55:15)

    you need the eval command to finish how you started:
    eval echo value=\$test$i
    Otherwise, use bash arrays:
    test=('data1' 'data2' 'data3')
    for i in $(seq 0 2);do # zero-based...
    echo value=${test[$i]}
    done
    ninja-edit to remove the comma's in the array
    Last edited by klixon (2009-10-06 19:27:03)

  • [SOLVED] Bash Script checking for mail

    I have come up with this forsaken script...
    #!/bin/bash
    while :
    do
    NEWMAIL=$(curl -su "You'd wish." https://mail.google.com/mail/feed/atom | grep fullcount | cut -d ">" -f 2 | cut -d "<" -f 1)
    echo $NEWMAIL
    notify-send "Gmail" "You have $NEWMAIL new mails!"
    sleep 10s
    done
    It does get the number of new mails perfectly fine - I'm sure there is a better method with cut... -, and it echos the number, but notify-send does not seem to work. From what I've read I need to set the environmental variable or something like that. Yeah, no clue. Any hint is appreciated.
    Last edited by ThunderRush (2013-02-05 14:38:15)

    I tried that aesiris, of course with some gnome things, but it did not work either.
    And for jason: It's okay, thank you again for your help.
    I will see what else I can do, and will post a solution if I find one - which I doubt.
    What other alternatives to notify-send are there? Not a whole popup, but something more subtle?
    #EDIT
    As always, my own stupidity.
    Since I had absolutely no right set on the file, since the password is there in clear text, ist had the Permissions 111.
    But - gnome did not belong to my root.
    ps aux | grep gnome
    And I always started it as sudo, as root. So I changed the ownership to my user...
    sudo chown thunderuser gmailcheck
    and set the rights to 500. So I can execute and read it.
    And it finally worked. Thanks again.
    Last edited by ThunderRush (2013-02-05 14:37:53)

  • [SOLVED][bash] read and tab character

    Hello archers!
    I'm trying to write simple script for mysql query results parsing. And I got that `read` command "eats" tab-characters:
    $ echo -e "foo\tbar\nbaz\tquuix\nfin" | while read L; do echo -e $L; done
    foo bar
    baz quuix
    fin
    i.e. tab characters were replaced with spaces.
    So, I can't perform the following:
    mysql -e "SELECT title,summary,body,added,modified FROM news" \
    | while IFS=\t read TITLE SUMMARY BODY ADDED MODIFIED
    do
    echo $TITLE
    done
    How can I prevent such behavior?
    Thanks!
    Last edited by pshevtsov (2012-05-23 09:42:13)

    pshevtsov wrote:
    So, I can't perform the following:
    mysql -e "SELECT title,summary,body,added,modified FROM news" \
    | while IFS=\t read TITLE SUMMARY BODY ADDED MODIFIED
    do
    echo $TITLE
    done
    How can I prevent such behavior?
    Thanks!
    If you want to use \t as IFS you have to quote it
    mysql -e "SELECT title,summary,body,added,modified FROM news" \
    | while IFS=$'\t' read TITLE SUMMARY BODY ADDED MODIFIED
    do
    echo $TITLE
    done

  • [SOLVED] Bash prompts and bell characters

    Hello
    This is kinda hard to explain, but I'll give a shot - with a couple of screenshots.
    Notice the terminal on the left, and also at the bottom.. whenever I type in a command (or paste from somewhere) that's longer than xx characters, and hit "END" to edit the command, it goes all the way to the start of the line - but visually it's leaving the character behind, so it's impossible to see where you're editing. I suppose it has something to do with my terminal size (even tho "fullscreen" in Xmonad).
    Other than that, I can't figure out why it does it, but it's really anoying.
    .Xdefaults
    http://codepad.org/U2VSyAUR
    Thanks for any help.
    Last edited by valvet (2010-11-07 18:30:05)

    linux-ka wrote:That's strange. I am using xmonad and rxvt-unicode-256color too, but there's no problem. I am running the current version of both of 'em. Have you upgraded xmonad/-contrib? or try to build your rxvt version on your own machine.
    Also, yes - my xmonad stuff should be up to date.
    Name : xmonad-darcs
    Version : 20101103-1
    URL : http://hackage.haskell.org/package/xmonad
    Licenses : custom:BSD3
    Groups : None
    Provides : None
    Depends On : gmp libxinerama ghc haskell-x11>=1.5.0.0 haskell-mtl
    Optional Deps : None
    Required By : xmonad-contrib-darcs
    Conflicts With : xmonad
    Replaces : None
    Installed Size : 4404.00 K
    Packager : Unknown Packager
    Architecture : i686
    Build Date : Wed 03 Nov 2010 11:13:51 PM CET
    Install Date : Wed 03 Nov 2010 11:14:03 PM CET
    Install Reason : Explicitly installed
    Install Script : Yes
    Description : A tiling window manager
    Name : xmonad-contrib-darcs
    Version : 20101103-1
    URL : http://hackage.haskell.org/package/xmonad-contrib
    Licenses : custom:BSD3
    Groups : None
    Provides : None
    Depends On : haskell-x11-xft xmonad-darcs
    Optional Deps : None
    Required By : None
    Conflicts With : xmonad-contrib
    Replaces : None
    Installed Size : 26100.00 K
    Packager : Unknown Packager
    Architecture : i686
    Build Date : Wed 03 Nov 2010 11:16:53 PM CET
    Install Date : Wed 03 Nov 2010 11:17:21 PM CET
    Install Reason : Explicitly installed
    Install Script : Yes
    Description : Third party extensions for xmonad
    Could you try with my Xdefaults file above, see if you can reproduce it? Also try using the font I am (Droid).
    Thanks
    Last edited by valvet (2010-11-07 02:04:57)

  • [SOLVED] Bash scripting - figure out how much you've downloaded

    Hi
    How would I aproach displaying how much (in mb/kb/gb whatever) I've downloaded?
    Thanks
    Last edited by valvet (2010-11-04 13:10:13)

    There's also http://www.archlinux.org/packages/?sort … =&limit=50
    [karol@black ~]$ vnstat -d
    eth0 / daily
    day rx | tx | total | avg. rate
    ------------------------+-------------+-------------+---------------
    10/02/10 525.89 MiB | 28.77 MiB | 554.66 MiB | 52.59 kbit/s
    10/03/10 850.30 MiB | 20.01 GiB | 20.84 GiB | 2.02 Mbit/s
    10/04/10 353.02 MiB | 20.26 MiB | 373.28 MiB | 35.39 kbit/s
    10/05/10 601.78 MiB | 42.41 MiB | 644.19 MiB | 61.08 kbit/s
    10/06/10 471.46 MiB | 26.29 MiB | 497.75 MiB | 47.19 kbit/s
    10/07/10 312.86 MiB | 15.24 MiB | 328.10 MiB | 31.11 kbit/s
    10/08/10 250.51 MiB | 13.85 MiB | 264.36 MiB | 25.07 kbit/s
    10/09/10 456.25 MiB | 22.86 MiB | 479.11 MiB | 45.43 kbit/s
    10/10/10 1.21 GiB | 43.15 MiB | 1.25 GiB | 121.82 kbit/s
    10/11/10 442.58 MiB | 21.45 MiB | 464.02 MiB | 44.00 kbit/s
    10/12/10 757.99 MiB | 33.45 MiB | 791.44 MiB | 75.04 kbit/s
    10/13/10 274.19 MiB | 18.22 MiB | 292.41 MiB | 27.72 kbit/s
    10/14/10 1.11 GiB | 48.94 MiB | 1.16 GiB | 112.59 kbit/s
    10/15/10 1.08 GiB | 42.71 MiB | 1.12 GiB | 108.95 kbit/s
    10/16/10 921.70 MiB | 32.01 MiB | 953.71 MiB | 90.43 kbit/s
    10/17/10 929.25 MiB | 37.68 MiB | 966.94 MiB | 91.68 kbit/s
    10/18/10 1.78 GiB | 73.96 MiB | 1.85 GiB | 179.53 kbit/s
    10/19/10 391.16 MiB | 19.23 MiB | 410.39 MiB | 38.91 kbit/s
    10/20/10 1.30 GiB | 42.81 MiB | 1.34 GiB | 130.33 kbit/s
    10/21/10 2.67 GiB | 77.57 MiB | 2.75 GiB | 267.06 kbit/s
    10/22/10 1.17 GiB | 36.96 MiB | 1.21 GiB | 117.29 kbit/s
    10/23/10 3.91 GiB | 104.04 MiB | 4.01 GiB | 389.56 kbit/s
    10/24/10 4.43 GiB | 124.12 MiB | 4.55 GiB | 441.50 kbit/s
    10/25/10 2.48 GiB | 71.18 MiB | 2.55 GiB | 247.22 kbit/s
    10/26/10 7.25 GiB | 146.55 MiB | 7.39 GiB | 717.75 kbit/s
    10/27/10 0.98 GiB | 35.87 MiB | 1.02 GiB | 98.78 kbit/s
    10/28/10 1.22 MiB | 599 KiB | 1.81 MiB | 0.17 kbit/s
    11/02/10 1.77 GiB | 30.93 MiB | 1.80 GiB | 174.33 kbit/s
    11/03/10 961.69 MiB | 44.55 MiB | 0.98 GiB | 95.41 kbit/s
    11/04/10 1.09 GiB | 44.35 MiB | 1.13 GiB | 170.49 kbit/s
    ------------------------+-------------+-------------+---------------
    estimated 1.69 GiB | 68 MiB | 1.76 GiB |
    [karol@black ~]$ vnstat -m
    eth0 / monthly
    month rx | tx | total | avg. rate
    ------------------------+-------------+-------------+---------------
    Feb '10 66.99 GiB | 4.72 GiB | 71.71 GiB | 248.65 kbit/s
    Mar '10 31.77 GiB | 2.68 GiB | 34.45 GiB | 107.89 kbit/s
    Apr '10 28.77 GiB | 2.50 GiB | 31.26 GiB | 101.18 kbit/s
    May '10 6.38 GiB | 682.79 MiB | 7.05 GiB | 22.08 kbit/s
    Jun '10 6.10 GiB | 347.45 MiB | 6.44 GiB | 20.83 kbit/s
    Jul '10 41.02 GiB | 1.73 GiB | 42.75 GiB | 133.89 kbit/s
    Aug '10 49.31 GiB | 2.91 GiB | 52.21 GiB | 163.52 kbit/s
    Sep '10 14.49 GiB | 939.38 MiB | 15.40 GiB | 49.85 kbit/s
    Oct '10 41.93 GiB | 21.36 GiB | 63.29 GiB | 198.21 kbit/s
    Nov '10 3.79 GiB | 119.83 MiB | 3.91 GiB | 104.14 kbit/s
    ------------------------+-------------+-------------+---------------
    estimated 31.21 GiB | 979 MiB | 32.17 GiB |

  • Unexpected token `(' in my bash script [Solved]

    I've been working on a bash script, and I'm trying to get it to move all directories that are not named certain names to another directory.
    EDIT: Fixed the thing papajoke pointed out
    #!/bin/bash
    mv ~/Downloads/!(folders|pics|docs|code|archives|vids|sounds)/ ~/Downloads/folders/
    The command from the script does what I want it to do when I run it from a terminal.
    It also works if I run the script as follows:
    source script.sh
    It doesn't work like this:
    bash script.sh
    I'm trying to get it to run when I login, and using the running the command with "source" in my MATE Startup Applications doesn't work.
    I'm new to bash scripting, any help would be much appreciated.  Thanks
    Last edited by physicsshark (2015-04-07 20:09:07)

    Trilby wrote:I've never seen that syntax
    You mean the pipes or the bang?
    $ touch a.gz b.gz c.txt
    $ ls !(*.gz)
    c.txt
    This works as an alias and from the comeliness commandline, but not in a script.
    $ ls -l ~/2
    total 16
    drwxr-xr-x 2 karol users 4096 Apr 7 03:13 a
    drwxr-xr-x 2 karol users 4096 Apr 7 03:07 b
    drwxr-xr-x 2 karol users 4096 Apr 7 03:07 c
    drwxr-xr-x 2 karol users 4096 Apr 7 03:13 d
    $ mv ~/2/!(a|b|c) ~/2/a
    $ ls -l ~/2
    total 12
    drwxr-xr-x 3 karol users 4096 Apr 7 03:14 a
    drwxr-xr-x 2 karol users 4096 Apr 7 03:07 b
    drwxr-xr-x 2 karol users 4096 Apr 7 03:07 c
    $ ls -l ~/2/a
    total 4
    drwxr-xr-x 2 karol users 4096 Apr 7 03:13 d
    Last edited by karol (2015-04-07 01:20:14)

  • Using Bash script to edit config file

    This is a really simple question, but given that I'm just learning Bash scripting and having this solved now would be really illustrative for me, I would really thank some help here.
    I'm using uzbl, and running Tor+Polipo. So, as you will see below in the tail of the config file, there is a line to redirect the requests of uzbl through Polipo.
    # === Post-load misc commands ================================================
    sync_spawn_exec @scripts_dir/load_cookies.sh
    sync_spawn_exec @scripts_dir/load_cookies.sh @data_home/uzbl/session-cookies.txt
    # Set the "home" page.
    #set uri = https://duckduckgo.com
    # Local polipo proxy
    set proxy_url = http://127.0.0.1:8123
    # vim: set fdm=syntax:
    What I want to accomplish is to comment in/out that line with a key shortcut on Awesome. I've thought of doing 2 scripts to do so and using 2 differente key shortcuts, but I want to "toggle" the proxy redirection with only 1 shortcut. To do so, I suppose that the script should go something like:
    if
    tool 'set proxy_url = http://127.0.0.1:8123' config_file
    then
    tool '#set proxy_url = http://127.0.0.1:8123' config_file
    else
    if
    tool '#set proxy_url = http://127.0.0.1:8123' config_file
    then
    tool 'set proxy_url = http://127.0.0.1:8123' config_file
    fi
    fi
    I know little about sed, but I think is the tool for this job. The most intriging part to me is to ask sed to print the regular expression when it finds it in the config file, and use that as an input in the conditional statement.
    Well, this is a mess I have done here. Hope there is a simple answer to this.
    Thanks in advance.-

    You can do this with a single sed command:
    sed -i 's/^#set proxy_url/set proxy_url/;
    t end;
    s/^set proxy_url/#set proxy_url/;
    : end' config_file
    This edits the file in-place (-i) and first tries to replace the commented with the uncommented line. If that suceeds, sed jumps to the "end" label. If not, it tries to replace the uncommented with the commented line. Thus you don't have to include any logic about the current state: if the first substitution succeeds, the line was obviously commented, if not, it was uncommented, and the second substitution should succeed.
    Note that my knowledge of sed is very limited. There might be a simpler way to do this.
    EDIT: For the sake of example, here's how to do the same in bash using regular expressions. Note how this script needs to use a temporary file to simulate in-place editing, how it needs to process the file line by line manually, etc. All things that sed does out of the box...
    #!/bin/bash
    tmp=test.conf.tmp
    echo -n "" > "$tmp"
    while read line; do
    if [[ "$line" =~ ^#set\ proxy ]]; then
    echo "${line/\#/}" >> "$tmp"
    elif [[ "$line" =~ ^set\ proxy ]]; then
    echo "#$line" >> "$tmp"
    else
    echo "$line" >> "$tmp"
    fi
    done < test.conf
    mv test.conf.tmp test.conf
    To answer your original question, the line
    if [[ "$line" =~ ^#set\ proxy ]]; then
    reads: if the line begins with a "#", followed by "set proxy", then...
    Last edited by hbekel (2011-03-20 10:40:16)

  • Sending email using bash script

    Hello:
    I am working on writing a bash script to notify one or more users by email of certain events. Run from the Terminal command line, and having the script "echo" text of (what would be) a form letter with in-line variable expansion (i.e., ${VARIABLE}), all seems to work as anticipated. Eventually, I want cron to launch this shell script, and send an email to an "on-subnet" user (I have postfix enabled on my Mac, and there are multiple local user accounts).
    I found some stuff on the web about sending mail from bash scripts, and so I made a small little test script, that reads like this:
    #!/bin/bash
    VARIABLE[1]="The 12,345 quick brown foxes "
    VARIABLE[2]="jumped over the 67,890 lazy dogs."
    mail -s "a test email" jv << EOF
    This is a test:
    ${VARIABLE[1]}
    ${VARIABLE[2]}
    This is the last line of the test message.
    EOF
    echo "script completed"
    It worked... almost... It sent a local email to my postfix mail account that read like this:
    This is a test:
    The 12,345 quick brown foxes
    jumped over the 67,890 lazy dogs.
    This is the last line of the test message.
    EOF
    echo "script completed"
    So, I have two questions. First, the easy one (I hope):
    How do I delimit the end of the text, that I want to be the message body of the email, from portions of the script that follow said email text?
    Next question is a little more involved. You know how, in Mail.app, if you go to Mail Preferences>Accounts>Account Information, you can put multiple email addresses, comma-delimited, in the "Email Address" field? So, if a person entered "[email protected], [email protected], [email protected]" in this field, then, even though (s)he may be at home, and using their home ISP's mail server, (s)he could send an email apparently from either their home, work, or school email address. Of course, the mail headers clearly would show it came from and through their home machine and home ISP, but it would be displayed in the recipient's Mail client viewer as having come from one of [email protected], [email protected], or [email protected].
    I'd like to do something similar here, whereby the email (that is being sent to one or more local users' postfix account on my computer) would apparently be sent from "watchdog@localhost" rather than from "jv@localhost" like it seems to do by default. Whatever account the script is run from (or presumbably, whose cron tab is launching the script) is what the "From" address is set to.
    I'd rather not create an additional mail account, because I am using Mac OS X built-in accounts for the postfix mailboxes (I don't want to have to maintain a plaintext username:password file in postfix, and I don't want to create an additional user account on the computer).
    So, is there a way to specify an alternate "From" username when invoking the mail -s ${SUBJECT} ${RECIPIENT} command in a bash script? Or is there a different, alternate mail command that will let me do so? (please include a description of syntax and how I'd package the above message text for the alternate method).
    Thanks in advance, all!

    Hi j.v.,
    The > after EOF is just a typo (or may be added by the Discussion ?) and you must delete it; other > are prompts from the interactive shell. Andy's post shows an interactive use of shell, not a shell script (note the shell prompt % in front of the commands). A typical use of here document may look like
    command <<ENDOFDATA
    ENDOFDATA
    There must be no spaces before and after ENDOFDATA. The word ENDOFDATA can be EOF or any other string which is guaranteed not to appear in the text (the .... in the example above).
    You can modify the From: header by using sendmail command (postfix has it as a compatibility interface):
    /usr/sbin/sendmail -t <<EndOfMessage
    Subject: test mail
    To: jv
    From: watchdog
    This is a test:
    ${VARIABLE[1]}
    ${VARIABLE[2]}
    This is the last line of the test message.
    EndOfMessage
    There must be a blank line between the headers and the mail body.
    I assume that you send these mails only to users on your local Mac. Please do not send mails to remote users by using the sendmail command unless you know what you are doing completely.
    PowerMac G4   Mac OS X (10.4.5)  

  • How do I save a "bash" script? (in attempt to fix one of my other problems)

    Here is my original problem:
    http://discussions.apple.com/thread.jspa?threadID=2195627&tstart=0
    I came across this that sounds like it will help my issue:
    http://www.macosxhints.com/article.php?story=20090316190817357
    However, what am I supposed to do with that code? Put it in the Terminal? Save it as an Apple Script? I also got the "lingon" program set up, all I need to do is figure how how to save this "bash" script and all set!
    Any ideas?
    -Scott

    This link might help as it shows how to create the plist using lingon:
    http://mymacfixes.blogspot.com/2009/06/how-do-i-stop-clicking-noise-from-hard.ht ml

Maybe you are looking for