(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)

Similar Messages

  • [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 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 initial directory problem

    Over the last couple of days, whenever I've opened any X virtual terminal (it doesn't do it when I ctrtl-alt-fX), it opened in a directory within my home directory, (namely the one I use for various programming projects), instead of the home directory. I thought that maybe if I removed that directory it might solve the problem, so I did, but apparently it didn't fix it, because now it outputs this:
    shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
    It outputs a subtly different message (chdir instead of shell-init) when I cd into a different directlry. I looked through my .bashrc, .bash_profile and .Xresources and none of them had anything relating to directories.
    Does anyone know how I could fix this?
    Last edited by alexanderthegre (2012-09-30 22:36:42)

    Did you launch X in that directory?
    sudo ls -dl /proc/$(pgrep Xorg)/cwd
    Or maybe the program you use to launch the terminal (WM, xbindkeys, etc)?

  • [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 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)

  • [solved][bash] PS1 wrapping problem...

    Hello I have my ps1 set to
    # Prompt
    BGREEN='\[\033[1;32m\]'
    GREEN='\[\033[0;32m\]'
    BRED='\[\033[1;31m\]'
    RED='\[\033[0;31m\]'
    BBLUE='\[\033[1;34m\]'
    BLUE='\[\033[0;34m\]'
    NORMAL='\[\033[00m\]'
    if [ "$TERM" = "linux" ]; then
    PS1="${NORMAL}[\u${BLUE}@\h ${BGREEN}\W${NORMAL}]\$ "
    else
    PS1="\e]2;\u@\H:\w\a${NORMAL}[\u${BLUE}@\h ${BGREEN}\W${NORMAL}]$ "
    fi
    However when my prompt displays [pyther@tux world.pyther.net]$ (color) I can only type 3 characters and then it wraps. Please see the screenshot...
    Using a regular prompt (source /etc/profile) works like a charm. Any help would be great!
    Last edited by pyther (2008-10-15 03:06:48)

    Daenyth wrote:Didn't I just answer this question in another thread? You need to escape the colors codes with \[ \] so that bash knows that they are non-printing characters, otherwise they will be counted in the prompt length.
    You're probably confusing this thread with mine a few days ago about using colors in PS1 in bash. ( http://bbs.archlinux.org/viewtopic.php? … 03#p432903 )
    This issue is very similar indeed
    Last edited by Dieter@be (2008-10-15 19:41:56)

  • [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 |

  • [SOLVED] problem with spaces and ls command in bash script

    I am going mad with a bash script I am trying to finish. The ls command is driving me mad with spaces in path names. This is the portion of my script that is giving me trouble:
    HOMEDIR="/home/panos/Web Site"
    for file in $(find "$HOMEDIR" -type f)
    do
    if [ "$(dateDiff -d $(ls -lh "$file" | awk '{ print $6 }') "$(date +%F)")" -gt 30 ];
    then echo -e "File $file is $(dateDiff -d $(ls -lh "$file" | awk '{ print $6 }') "$(date +%F)") old\r" >> /home/panos/scripts/temp;
    fi
    done
    The dateDiff() function is defined earlier and the script works fine when I change the HOMEDIR variable to a path where there are no spaces in directory and file names. I have isolated the problem to the ls command, so a simpler code sample that also doesn't work correctly with path names with spaces is this:
    #!/bin/bash
    HOMEDIR="/home/panos/test dir"
    for file in $(find "$HOMEDIR" -type f)
    do
    ls -lh "$file"
    done
    TIA
    Last edited by panosk (2009-11-08 21:55:31)

    oops, brain fart. *flushes with embarrassment*
    -- Edit --
    BTW, for this kind of thing, I usually do something like:
    find "$HOMEDIR" -type f | while read file ; do something with "$file" ; done
    Or put those in an array:
    IFS=$'\n' ; files=($(find "$HOMEDIR" -type f)) ; unset IFS
    for file in "${files[@]}" ; do something with "$file" ; done
    The later method is useful when elements of "${files[@]}" will be used multiple times across the script.
    Last edited by lolilolicon (2009-11-09 08:13:07)

  • [solved] Running a command in background (bash script)

    Salut,
    as netcfg2 does not work with my wireless connection, I have to set up the connection manually. For not having to type in the commands every time, I created a bash script.
    #!/bin/bash
    iwconfig wlan0 mode managed essid mynetwork channel 6
    ifconfig wlan0 up
    wpa_supplicant -D wext -i wlan0 -c /etc/wpa_supplicant.conf -dddd &
    dhcpd wlan0
    This works fine till the wpa_supplicant line. wpa_supplicant is not started in the background (as I thought, the ampersand at the end of the line would.
    So how can I get wpa_supplicant run in the background?
    Thanks in advance,
    Stefan
    Last edited by vbtricks (2008-05-11 09:13:36)

    Ramses de Norre wrote:How do you know it isn't? What exactly does happen?
    The script does not return to the user-prompt. Is an ampersand at the end of the line the correct solution, or are you unsure yourself?
    bender02 wrote:On thing is that even if it starts, it does take it a couple of seconds to connect, so it's probably not very good to run dhcpcd right after wpa_supplicant. Another thing is that you probably have a typo up there, shouldn't it be 'dhcpcd' instead of 'dhcpd'?
    Well, as the wpa_supplication command is not really run in the background the script did never execute the dhcpcd command. After having solved the above, correcting the spell-mistake will be a smaller problem. Even calling the dhcpcd command myself would be no unworkable way, the open root-shell (as I have to use another as the one calling the script is blocked) is a far greater problem...

  • 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

  • Simple bash script to add a '-' [Solved]

    I need to write a small bash script to add a '-' to each line in a file before displaying via conky!
    Todo
    - Get Milk
    - Buy Food
    - Pay Bills
    Currently I use
    TEXT
    Todo
    ${hr}
    ${head /home/mrgreen/.stuffigottado.txt 30 20}
    In .conkyrc but have to add '-' each time I edit .stuffigottado.txt
    Thanks in advance....

    Cerebral wrote:
    To filter out blank lines, you could just modify the awk command:
    ${exec awk '!/^$/ { print "-", $_ }' stuffigottado.txt}
    very nice; awk and grep: two commands that never cease to amaze me.

  • Problem with "date -d" function in bash script. Leopard bug?

    I have this really nice rsync bash script that I use to make rotating snapshot backups of my different Ubuntu servers to a single backup server.
    I am now trying to use this same script on my Leopard machine, but it seems to have a serious problem with the following lines of code:
    lday=`date +'image-%Y-%m-%d-%H' -d '1 day ago'`
    lweek=`date +'image-%Y-%m-%d-%H' -d '1 week ago'`
    It generates the following error:
    date: illegal time format
    usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
    [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
    It's clear that it doesn't like the "-d '1 day ago'" part in my code.
    Is there a different way in Leopard to get yesterday's or last week's date in a bash script? Or is this a Leopard bug?
    Could someone please help me out? I've be trying to research this for the past 8 hours, but can't seem to find any solution to this problem.
    Thanks in advance.

    Is there a different way in Leopard to get yesterday's or last week's date in a bash script?
    date -v-1d
    date -v-7d

  • Bash script - sqllite insert problem

    So, I'm making a bash script that inserts a config file into a database, but I can't get it to work.
    This is my best attempt so far:
    name="bashrc"
    location="/home/user/.bashrc"
    content=`cat "$location"`
    sqlite3 testdb "insert into dbase values ('`echo $name`','`echo "$location"`','`echo "$content"`')"
    the table is
    create table dbase(name text, location text, data text)
    I had a hard time trying to figure out how I'd be able to get $variables to work (because of the quotes) and this echo style seems to work, but there seems to be no way to insert the config file into the database.
    plaintext files seem to work no problem, but when I'm trying to insert some config file, it spits out errors. (It seems like it's trying to execute the file somehow?)
    Last edited by initbox (2009-08-29 15:28:49)

    initbox wrote:I was thinking the single quotes were stopping bash from replacing $variables with the content, hence the echoes.
    your single quotes are inside a double quote, so to bash they're just another character in the string.
    I'm still getting an SQL syntax error, so bash is no longer getting in the way, but this thing is a true nightmare. What next? This turned out to be way harder than I expected, I'm just doing this as a "fun" learning experiment.
    Your error is probably because I was escaping the quotes for bash, and I should have been escaping them for sqlite. In sqlite, escaping a quote is done by doubling it, so ' is escaped to '' (that's two apostrephes side by side, not a double quote. Compare '' and ").
    This untested sed command might work:
    content = $(cat $location | sed "s/\'/\'\'/g")
    The \'s are escaping the quote from bash, what its really doing is replacing ' with ''.
    Dusty

Maybe you are looking for

  • My Mac keeps logging me out of my account

    It started this morning immediately after I turned on my Mac Pro. I have 3 accounts on the MP, and I logged in to my usual account, after about one second, after the desktop loads etc, it logs me out automatically. This keeps happening. I am able to

  • Filter Master data

    Hi, Question: Is it possible to filter the List of Business Partner data with help of SDK? The customer like to select data for specific employees. Thank you, Rune

  • Load balancing and session sharing

    Hi, in my company we have the following situation: We have 2 instances of the same web application on differend servers. An external load balancer (Alteon) routes the requests to these instances. The problem: If one instance is shutting down, all the

  • HT4858 How do I set up iCloud on XP

    I use iCloud on an iPhone, iPad, Macbook Air, and Windows * PC.  My new hospital computer is running XP.  I was able to download iCloud, but it doesn't sync with other iCloud based programs.

  • IMac versus Mac Pro

    Hello all, I think the time has come for an upgrade. I have a Mac Pro that is about 6 years old. When I got it I was doing editing with FCP, 3D work with Poser, Vue Cinema 4D, etc. and graphics with Adobe Production Suite with After Effects. Now I do