SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2013-01-15 11:39:12

fortyseven
#! Die Hard
From: Sector 7G
Registered: 2011-12-23
Posts: 509

[SOLVED, for now] Help with a Bash Script, Please

I don't have much time to spend on this and since my bash scripting isn't what it could be, I thought I'd ask some of the CB friends for a helping hand.

I have a new job where every day i have to scan documents and sort them into various directories on a server. Currently I'm doing it one at a time, cutting and pasting. Could some of you bash geniuses help me with a way of doing it automatically?

The scanned document gets a file name based on the date, ie:

ABC_dc_20130115.pdf

DEF_dc_20130115.pdf

It then needs to go into a directory on a samba share, ie:

Customer/Calibrate/ABC/all/support/

Customer/Calibrate/DEF/all/support/

Hope I'm being clear enough. Please help.

PLEASE SEE POST 3.

Last edited by fortyseven (2013-01-21 11:08:55)


on the outside looking in

Offline

Be excellent to each other!

#2 2013-01-15 11:52:58

rahmu
Member
From: /home
Registered: 2011-04-16
Posts: 15
Website

Re: [SOLVED, for now] Help with a Bash Script, Please

Something like this?

#!/bin/bash 
# or whatever shell you're using

for doc in $@; do
    echo mv "$doc" Customer/Calibrate/"${doc%%_*}"/all/support
done

The script will move each file given as an argument in the command line, to the Customer/Calibrate/[all letters until the first "_"]/all/support. I added the echo call so that you can confirm that this is what you're looking for.

For instance:

$ bash test.sh ABC_dc_20130115.pdf DEF_dc_20130115.pdf
mv ABC_dc_20130115.pdf Customer/Calibrate/ABC/all/support
mv DEF_dc_20130115.pdf Customer/Calibrate/DEF/all/support

Can you confirm this answers your question? We could add some tests and error reporting to make it more robust and decrease its chance to fail.


Turtles have it figured out, man

Offline

#3 2013-01-15 14:30:00

fortyseven
#! Die Hard
From: Sector 7G
Registered: 2011-12-23
Posts: 509

Re: [SOLVED, for now] Help with a Bash Script, Please

For now, if possible, I'd like to put this script on hold and ask for help with something more urgent.

My boss, who I believe is testing me, has asked me to create a script that lists the versins of certain binaries and .csv files. My scripting skills are currently at about a 1 out of 10 so I guess it's time to learn. I don't however have access to the internet at home and at work, my browsing is severly limited. So again I ask for help.

As said, the script should be able to list the version of certain binaries, located in a directory called /AMMO. The script should also list the versions of certain .csv files located in a directory called /AMMO/conf.

Now to get the version number of the binaries I use the command:

./binaryone --version

And to get the version of the csv files I simply use:

head -n 1 filename.csv

How would I go about putting all this together in a script?

EDIT: Oh, and the output of all this must go into another .csv file.

Last edited by fortyseven (2013-01-15 14:37:46)


on the outside looking in

Offline

#4 2013-01-16 09:26:44

fortyseven
#! Die Hard
From: Sector 7G
Registered: 2011-12-23
Posts: 509

Re: [SOLVED, for now] Help with a Bash Script, Please

any thoughts? any one?


on the outside looking in

Offline

#5 2013-01-16 13:30:01

xaos52
The Good Doctor
From: Planet of the @s
Registered: 2011-06-24
Posts: 4,602

Re: [SOLVED, for now] Help with a Bash Script, Please

#!/bin/bash
set +e

# Replace the execdirs value by a list of space separated directories to search
execdirs="$HOME/tmp/scripts"

function handle_files () {
        for file in $@; do
                if [[ ! -x $file ]]; then continue; fi
                echo "$( $file --version )"
        done
}

if [[ $# -gt 0 ]]; then
        handle_files $@
        exit 0
fi

for dir in $execdirs; do
        # find all files, then call the script again with the file list
        # limit is set to 100 files at a time ; If there are more than
        # 100 files the script will be called multiple times.
        find $dir -type f -print0 | xargs -0 -i -L 100 $0 -x
        # Handle csv files in conf directory
        if [[ -d $dir/conf ]]; then
                for file in $dir/conf/*; do
                        echo $file
                        if [[ "${file##*.}" = "csv" ]]; then
                                echo "$( head -n1 ${file} )"
                        fi
                done
        fi
done

This script should do what you asked for.
Read  the comments.
The results are directed to sysout.
To capure the output run the script as
./script > results

WARNING: the script will run every executable file in the directories you specify with the --version argument.
If the script does not know the version argument, it will be called too. This may produce results that you did not expect.

For instance if there is a script there that contains the single command
rm -rf /
It will happily remove your entire file structure.

Use with discretion. At your own risk.

Last edited by xaos52 (2013-01-16 13:31:50)

Offline

#6 2013-01-17 05:51:25

fortyseven
#! Die Hard
From: Sector 7G
Registered: 2011-12-23
Posts: 509

Re: [SOLVED, for now] Help with a Bash Script, Please

^ Thank you for your response and time. I played around with the script you provided but unfortunately it's a bit too complicated. Allow me to explain. I work for a company that produces a device which runs a very small, very stripped down (and very old) version of Debian. A lot of the common elements one finds in a modern distro are not included in this version. As a result your script seems a little too 'advanced' so I need something a lot simpler.

I was playing around with something straight forward like this:

#!/bin/bash

/AMMO/binaryone --version > /AMMO/conf/version.csv
head -n1 /AMMO/conf/GramS.csv > /AMMO/conf/version.csv
head -n1 /AMMO/conf/Map.csv > /AMMO/conf/version.csv

It works fine but the problem I'm having is that the output end up like this:

binaryone 3.5.6
GramS 1.01
Map 2.023

I need the output to look like this:

binaryone 3.5.6, GramS 1.01, Map 2.023

on the outside looking in

Offline

#7 2013-01-17 09:48:50

xaos52
The Good Doctor
From: Planet of the @s
Registered: 2011-06-24
Posts: 4,602

Re: [SOLVED, for now] Help with a Bash Script, Please

#!/bin/bash

echo -n $(/AMMO/binaryone --version) > /AMMO/conf/version.csv
echo -n ", $(head -n1 /AMMO/conf/GramS.csv)" >> /AMMO/conf/version.csv
echo -n ", $(head -n1 /AMMO/conf/Map.csv)" >> /AMMO/conf/version.csv

This, perhaps?

Offline

#8 2013-01-17 13:57:28

fortyseven
#! Die Hard
From: Sector 7G
Registered: 2011-12-23
Posts: 509

Re: [SOLVED, for now] Help with a Bash Script, Please

Thanks, xaos. I eventually got it with:

echo -n `/AMMO/binaryone --version`>>/AMMO/conf/version.csv ; echo -n ','>>/AMMO/conf/version.csv
echo -n `head -n 1 /AMMO/conf/GramS.csv`>>/AMMO/conf/version.csv ; echo -n ','>>/AMMO/conf/version.csv 

Thank you for your time. Appreciated.


on the outside looking in

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo