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

Similar Messages

  • Conky doesn't display bash-script variables (array)

    I've been playing around with Conky and a bash script of mine. Unfortunately Conky displays only static text and not the array-variables in my script.
    In my script:
    ...some code here...
    echo "Static text: ${Variable[1]}"
    In my conkyrc:
    ...some code here...
    ${exec ~/Test/test.sh}
    The result is: "Static text: ". When running the script from the command line everything is fine. I've also tried with exec, execi, execp, texeci to no avail. Any ideas?
    Edit: I had to be more specific.
    Last edited by chilebiker (2009-10-06 03:30:20)

    Try echo -n "Static text: ${Variable[1]}"

  • [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] FFMPEG appears to be trashing bash script variables

    Hi,
    I have a directory containg MP4 files which were downloaded via youtube-dl upon which I want to extract audio using FFMPEG.
    I am using the usual find/while/read/do/done loop to process each file individually. The files were downloaded using Youtube-dl (invoked with --restrict-filenames) so the youtube video ID is recorded within the filename which can then be used to obtain the video title (ie to tag the audio with once extracted, ie ID3).
    Without FFMPEG the loop works as expected as can be seen when using echo, but as soon as I introduce FFMPEG into the loop, no such file or directory errors are reported because the filenames passed to FFMPEG are nonsense and appear to have been trashed, for instance, characters are stripped from the start or end of the filename.
    find . -name '*.mp4' | while read FILE
    do
    FNAME=${FILE%.mp4}
    FID=${FNAME: -11}
    FTITLE=${FNAME%-$FID}
    TITLE=$(youtube-dl --get-title $FID)
    # MP3="${FNAME}.mp3"
    AAC="${FNAME}.aac"
    ffmpeg -i $FILE -vn -acodec copy $AAC
    done
    Frequently green text like so reams down the terminal:
    stream #1:
    keyframe=1
    duration=0.023
    dts=355.033 pts=355.033
    size=252
    Swiftly followed by:
    00000000 21 0a 4f ff ff e0 3f fe a2 c6 b1 41 31 a0 88 41 !.O...?....A1..A
    00000010 28 3e 00 03 d2 15 25 4a 92 17 40 0b e4 f5 4c e5 (>....%[email protected].
    00000020 af 56 45 aa 31 0c eb 87 45 ac 26 54 8a ed c9 b4 .VE.1...E.&T....
    00000030 94 98 ca a8 03 34 aa bf 04 51 38 12 c9 43 80 62 .....4...Q8..C.b
    00000040 5b 16 94 99 20 18 9e cd 55 d0 6c 15 38 13 ce cc [... ...U.l.8...
    00000050 d4 2e 80 3b 10 e9 07 70 bc 23 8c 8c 84 96 8a 38 ...;...p.#.....8
    00000060 29 63 8d 87 75 5e 66 dc fa de 7f 4e 70 b2 44 09 )c..u^f....Np.D.
    00000070 39 c7 30 50 78 2b 6a b0 48 90 ba c4 14 c4 41 5c 9.0Px+j.H.....A\
    00000080 b6 3c 64 38 4d 92 3a b6 d1 01 d3 99 86 d5 54 23 .<d8M.:.......T#
    00000090 2a c4 13 49 61 50 b7 d0 3c 1e 2b 4b 90 c4 32 ae *..IaP..<.+K..2.
    000000a0 bc 6b 57 a6 de df 19 53 8e 38 66 d1 c4 fc a4 d5 .kW....S.8f.....
    000000b0 f9 6f a9 2d 20 6c eb 29 1e 1a ef 96 a6 84 82 63 .o.- l.).......c
    000000c0 0a cc 75 82 14 b4 08 09 fa 11 b8 0d e7 11 80 84 ..u.............
    000000d0 20 31 18 00 05 2a 54 af 46 ac 08 79 b9 a8 11 70 1...*T.F..y...p
    000000e0 f5 c0 2f 88 4b 27 7c a4 fd f9 f1 e4 77 53 c5 4a ../.K'|.....wS.J
    000000f0 d7 74 d1 02 80 46 40 08 16 a1 e7 .t...F@....
    It doesn't matter if the filenames originate from find or redirected from the contents of a text file. Anyone have any suggestions for workarounds?
    Last edited by jwm-art (2014-05-27 21:39:39)

    Self contained example:
    #!/bin/bash
    while read URL
    do
    echo -n "Downloading video from ${URL}... "
    youtube-dl --restrict-filenames "${URL}" --quiet
    if test $? -eq 0; then
    echo "Ok"
    else
    echo "FAIL"
    echo "Aborting..."
    exit
    fi
    done <<YT_URLS
    https://www.youtube.com/watch?v=XqdYnxv01yM
    https://www.youtube.com/watch?v=lX_o5t2YoUE
    https://www.youtube.com/watch?v=V9sI6VEDE5M
    YT_URLS
    find . -name '*.mp4' | while read -r FILE
    do
    echo "-----------------------------------------"
    echo -n "Converting ${FILE} to MP3... "
    MP3="${FILE%.mp4}.mp3"
    echo "${MP3}"
    ffmpeg -loglevel warning -y -i "${FILE}" "${MP3}"
    if test $? -eq 0; then
    echo "Ok"
    else
    echo "FAIL"
    echo "Abortingm..."
    exit
    fi
    echo
    done
    Here's the output on my system:
    $ ../ffmpeg_test
    Downloading video from https://www.youtube.com/watch?v=XqdYnxv01yM... Ok
    Downloading video from https://www.youtube.com/watch?v=lX_o5t2YoUE... Ok
    Downloading video from https://www.youtube.com/watch?v=V9sI6VEDE5M... Ok
    Converting ./Rabbit_City_White_Lable_-_Beyond_Control-lX_o5t2YoUE.mp4 to MP3... ./Rabbit_City_White_Lable_-_Beyond_Control-lX_o5t2YoUE.mp3
    Enter command: <target>|all <time>|-1 <command>[ <argument>]
    Parse error, at least 3 arguments were expected, only 1 given in string 'osmo_Dibs_-_Xultation-V9sI6VEDE5M.mp4'
    debug=1
    debug=2 1984kB time=00:02:06.95 bitrate= 128.0kbits/s
    error parsing debug value
    debug=0
    [output stream 0:0 @ 0xd90b60] EOF on sink link output stream 0:0:default.
    No more output streams to write to, finishing.
    [libmp3lame @ 0xc71de0] Trying to remove 815 more samples than there are in the queue
    size= 6971kB time=00:07:26.12 bitrate= 128.0kbits/s
    video:0kB audio:6971kB subtitle:0 data:0 global headers:0kB muxing overhead 0.004483%
    19212 frames successfully decoded, 0 decoding errors
    [AVIOContext @ 0xd9ff60] Statistics: 2 seeks, 17081 writeouts
    [AVIOContext @ 0xc77200] Statistics: 7082450 bytes read, 176 seeks
    Ok
    The first MP4 file found is converted, but alongside strange unexplained output, and then it just stops. In the first iteration of the second loop how does FFMPEG get hold of the second filename (incomplete, missing first character)? What is the "Enter command" all about? Its all very weird.
    Last edited by jwm-art (2014-05-27 20:17:57)

  • [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 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 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 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]bash: assign a command output to a variable

    EDIT: Sorry, got the solution - the variable was good, I just forgot a "do" in a "for" loop.... Sorry
    Hi erveryone.
    I'm just starting to learn the basics of bash scripting, and I got a problem assigning the output of a command to a variable, and i keep getting syntax errors...
    var=$(cut -d: -f1 foo)
    echo $var
    A google search gives many different ways to do this, but without better results..
    var=`cut -d: -f1 foo`
    var="`cut -d: -f1 foo`"
    Any idea what I'm doing wrong?
    Thanks!
    Last edited by haiku (2012-02-01 14:19:00)

    haiku wrote:var=$(cut -d: -f1 foo)
    var=`cut -d: -f1 foo`
    The first two are both 'right' (in bash) but the first is the preferred syntax (in bash).  I think sh only understands the second.  I've never used quotes in a situation like this, but it seems to work.  That said, maybe the problem is the command itself, because all variations work for me:
    $ cat foo
    genmon1a:genmon1b
    genmon2:genmon3:genmon4
    $ cut -d: -f1 foo
    genmon1a
    genmon2
    $ D=$(cut -d: -f1 foo)
    $ echo $D
    genmon1a genmon2
    $ D=`cut -d: -f1 foo`
    $ echo $D
    genmon1a genmon2
    $ D="$(cut -d: -f1 foo)"
    $ echo $D
    genmon1a genmon2
    $ D="`cut -d: -f1 foo`"
    $ echo $D
    genmon1a genmon2
    It might help if you could give us some of the 'bigger picture' too: what you are trying to do overall, the contents of foo, what you are trying to do with the variable, etc.
    Last edited by alphaniner (2012-02-01 14:31:01)

  • [DONE] Bash scripting. A few questions

    Hi,
    I've created my own custom sleep script as it is described here: https://wiki.archlinux.org/index.php/Sy … stem-sleep and it's already working correctly.
    However I've got a few questions concerning bash scripting since I've never did it before.
    Here the example from the wiki:
    #!/bin/sh
    case $1/$2 in
    pre/*)
    echo "Going to $2..."
    post/*)
    echo "Waking up from $2..."
    esac
    Now what do I need the /*) for in the cases pre and post? What does it do? And why isn't there any *) in the end which would exit the script if none of the above is true?
    Secondly, why does it say case $1/$2 in, but not case $1 in, what is the $2 needed for? E.g. in this script only $1 is used: https://wiki.archlinux.org/index.php/OS … ibernation
    And third question is, if I do really need the double quotes around $1? Does it make any difference?
    Sorry for my incompetence but I'm not a dev and as I already said I've never did any bash scripting...
    Thanks in advance
    best regards
    nuc
    Last edited by nuc (2013-02-07 18:10:56)

    ok, here's my actual code:
    #!/bin/sh
    suspend_osssound()
    /usr/lib/oss/scripts/killprocs.sh
    /usr/sbin/soundoff
    resume_osssound()
    /usr/sbin/soundon
    case "$1" in
    pre)
    suspend_osssound
    post)
    resume_osssound
    *) exit $NA
    esac
    It is the same as the one in the OSS wiki entry, but slightly configured to work under logind and not pm-utils. Of course the new location is /usr/lib/systemd/system-sleep/.
    I'm about to update the wiki with my code, so I'd like to know if there aren't any major mistakes. I tested it and it works but I want to get sure
    PS: Do people actually still use pm-utils for suspending or can I safely remove the previous script from the wiki?
    EDIT: Added double quotes
    Last edited by nuc (2013-01-25 23:42:02)

  • Questions concerning basic bash scripting

    Good evening,
    at the moment we do bash scripting at university, but I seen to encounter some problems in understanding the functionality of bash. I would be very grateful if you guys could help me out with one or another question.
    First one:
    Task to accomplish: Scripts with three arguments. $1 -> source directory, $2 -> file suffix, $3 -> target directory. Search source directory for all files with suffix $2 and copy them to $3.
    I tried to do it, using pipe and xargs, but I am wondering how to pass the filenames found over the pipe.
    find $source -name "*.$suffix" | xargs cp [MISSING ARGUMENT] $target
    How do I access the data I need to fill in as first argument of cp, or isn't it possible to accomplish the task this way?
    Regards,
    cg

    chaosgeisterchen wrote:find $source -name "*.$suffix" | xargs cp [MISSING ARGUMENT] $target
    Almost, you just need the -t flag to cp which basically reverses the order of SOURCE and TARGET in this instance
    find $source -name "*.$suffix" | xargs cp -t $target
    Also, I suggest you escape the * in your find to prevent bash expanding it. Bash shouldn't expand, find should.
    You might like to also use the -iname (as opposed to -name) predicate to find if you don't care about the case of the source files.
    Last edited by fukawi2 (2008-06-02 03:13:37)

  • [solved]Need help with a bash script for MOC conky artwork.

    I need some help with a bash script for displaying artwork from MOC.
    Music folders have a file called 'front.jpg' in them, so I need to pull the current directory from MOCP and then display the 'front.jpg' file in conky.
    mocp -Q %file
    gives me the current file playing, but I need the directory (perhaps some way to use only everything after the last  '/'?)
    A point in the right direction would be appreciated.
    thanks, d
    Last edited by dgz (2013-08-29 21:24:28)

    Xyne wrote:
    You should also quote the variables and output in double quotes to make the code robust, e.g.
    filename="$(mocp -Q %file)"
    dirname="${filename%/*}"
    cp "$dirname"/front.jpg ~/backup/art.jpg
    Without the quotes, whitespace will break the code. Even if you don't expect whitespace in any of the paths, it's still good coding practice to include the quotes imo.
    thanks for the tip.
    here it is, anyhow:
    #!/bin/bash
    filename=$(mocp -Q %file)
    dirname=${filename%/*}
    cp ${dirname}/front.jpg ~/backup/art.jpg
    then in conky:
    $alignr${execi 30 ~/bin/artc}${image ~/backup/art.jpg -s 100x100 -p -3,60}
    thanks for the help.
    Last edited by dgz (2013-08-29 21:26:32)

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

Maybe you are looking for

  • "error while saving image"

    has anyone had the problem where, when they select option and tap an image (in my case i pulled one from my email using an image i uploaded from my drive) and gotten an error that says "there was an error saving image" and your only option is 'ok'? i

  • Problem with Automated Tasks

    Hi all, At this moment I'm testing a cartridge deployed in OSM7 that is installed in Solaris Spark. The cartridge contains processes that include Automated Tasks with XQuery Sender Automator Internal to send a message to a queue JMS. In some of the A

  • Email Text Content Issue

    Hello, I am working on issue of removing some junk characters from the email our business receive. Here is the details of the text - Text send via java program (using JAVA Mail API). code snippet - private static String trademark_logo= "\n\nOurbusine

  • Auto more not working

    Hello all, From the ECB IT dept, having an issue with a users 8800. When the user views emails, it only displays the the first part, to view the rest user must click on more all. Did some research and found out about more all, set the user blackberry

  • Quicktime pro installation

    All I really want to do is import a QT film from final cut pro to imovie so I can burn a DVD. Imovie informs me that the project is too long to import (6minutes????) and that I have to go to quicktime pro to do something that will (compress it???) or