LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-16-2013, 07:30 AM   #1
dimitriauxio
LQ Newbie
 
Registered: Sep 2013
Posts: 6

Rep: Reputation: Disabled
Bash Script passing arguments


I am new to Linux and scripting and I will try to explain in further detail the problem that I have been stack to.

I have written a script that we are passing arguments to it when we execute it:

./windup.sh User Password Host Path

When the user hits enter the script is doing the following:

The script finds two different types of files (.ear and .war) into a directory (/ftpuser) and then it checks if these files have the same name but different type (.config) as well. For example if the .ear file that we have found in the /ftpuser directory is abc1.ear then it has to check if there is a abc1.config. When it finds those files then it executes one command in which we are passing some values from the .config file we have found previously. This command is generating reports of the files that we have found previously into a directory called /reports. Then it copies the file that we have found in the /ftpuser directory and also the report file, to a directory called /scanned and also is making a secure copy of these files into the path directory we are inserting when we execute the windup.sh. This path can be accessed with the username and password that the user entered before.

Below I am providing you what I have done so far:
Code:
set -vx
User=$1
Password=$2
Host=$3
Path=$4
for i in $(grep "^packages" $filename | cut -d= -f2 | tr ',' ' '); do
for file in $(find /home/user/ftpuser -maxdepth 1 -iname -type f | while read ITEM; do
case "${ITEM}" in
    *.ear|*.war) if [ -e "${ITEM%.ear}.config" -o -e "${ITEM%.war}.config"  ]; then
        echo got .config
            fi
        ;;
esac); do
echo /apps/oracle/jrockit/4.1.0-1.6.0_37-R28.2.5-x86_64/bin/java -jar ../windup-cli-0.6.8/windup-cli.jar -javaPkgs ${i} -input ../ftpuser/ -output ../reports/ "$file"
    cp "${file}" /home/user/ftpuser/scanned/
    scp "${file}" (directory to the specified path from the user,needs login details)
done
sleep 60
done
When we open the .config file we see thes values inside:
packages=com.ibm,com.jboss,com.sun

I am completely lost. I am new to bash and I cant glue all these tasks together. Is anyone able to help me to sort out this problem?

Last edited by dimitriauxio; 09-16-2013 at 09:23 AM.
 
Old 09-16-2013, 09:04 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,501

Rep: Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431
please use [code]here comes your script[/code] to keep formatting your original code
add set -vx at the beginning of the script to see what's happening.
 
Old 09-16-2013, 09:24 AM   #3
dimitriauxio
LQ Newbie
 
Registered: Sep 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
hope that the changes will help you
 
Old 09-16-2013, 09:32 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,501

Rep: Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431
yes, it is now much better. Have you tried to run this script (with set -xv)? You need to see what's happening. I think something is not ok with the second for (for file in ...), but you need to check if the variable file has got proper values.
 
Old 09-16-2013, 09:34 AM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
please use code tags for code
https://www.linuxquestions.org/quest....php?do=bbcode

Code:
User=$1
Password=$2
Host=$3
Path=$4

for i in $(grep "^packages" $filename | cut -d= -f2 | tr ',' ' '); do
                            # ^ undefined var.
    for file in $(find /home/user/ftpuser -maxdepth 1 -iname -type f | while read ITEM; do
        case "${ITEM}" in
           *.ear|*.war) if [ -e "${ITEM%.ear}.config" -o -e "${ITEM%.war}.config" ]; then
                            echo got .config
                        fi
                        ;;
                  esac
                 ); do # this is very confused 
        echo /apps/oracle/jrockit/4.1.0-1.6.0_37-R28.2.5-x86_64/bin/java -jar ../windup-cli-0.6.8/windup-cli.jar -javaPkgs ${i} -input ../ftpuser/ -output ../reports/ "$file"
                                                                                                                                                                      # ^ is either "got" or ".config"
        cp "${file}" /home/user/ftpuser/scanned/
            # ^ is either "got" or ".config"
        scp "${file}" (directory to the specified path from the user,needs login details)
            # ^ is either "got" or ".config"
    done
    sleep 60
done
I'm not clear on what you want to do
I think $i should feature in your find command
the for file, is redundant
the echo, cp and scp should be inside the case, on the condition that the .config exists

many holes in the below, as I do not really understand what you need
Code:
#!/bin/bash
User=$1
Password=$2
Host=$3
Path=$4

filename=/path/to/somefile

for i in $(grep "^packages" $filename | cut -d= -f2 | tr ',' ' '); do
    find /home/user/ftpuser -maxdepth 1 -iname -type f -iname "*$i*" | while read ITEM; do
        case "${ITEM}" in
           *.ear|*.war) if [ -e "${ITEM%.ear}.config" -o -e "${ITEM%.war}.config" ]; then
                            echo got .config
                            echo /apps/oracle/jrockit/4.1.0-1.6.0_37-R28.2.5-x86_64/bin/java -jar ../windup-cli-0.6.8/windup-cli.jar -javaPkgs ${i} -input ../ftpuser/ -output ../reports/ "$ITEM"
                            cp "${ITEM}" /home/user/ftpuser/scanned/
                            scp "${ITEM}" (directory to the specified path from the user,needs login details)
                        fi 
                     ;;
        esac
    done
    sleep 60
done


http://tldp.org/LDP/Bash-Beginners-Guide/html/
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/BashGuide


re-read, and re-wrote

you don't need the case, case is really just a more convenient way of doing nested if statements

to make more readable, used $Windup for most of the command line

used the User var in the find
I assumed /reports and /ftpuser were on the root ( if not 'build' them with reports_dir=/home/${User}/reports )

Still needs 'work', I have left out error capture/reporting ( added hint )
Code:
#!/bin/bash
User=$1
Password=$2 # bad!
Host=$3
Path=$4
filename=/path/to/somefile

Windup="/apps/oracle/jrockit/4.1.0-1.6.0_37-R28.2.5-x86_64/bin/java -jar ../windup-cli-0.6.8/windup-cli.jar"

for Package in $(grep "^packages" $filename | cut -d= -f2 | tr ',' ' '); do
    find /home/${User}/ftpuser -maxdepth 1 -iname -type f -iname "*${Package}*[ew]ar" | while read File;do
        if [[ -e "${File%.[ew]ar}.config" ]]; then
           echo got .config
           $Windup -javaPkgs ${Package} \
                   -input "/ftpuser/${File%.[ew]ar}.config" \
                   -output "/reports/${File%.[ew]ar}.report" || echo this is a failure hint
           cp "${File}" "/reports/${File%.[ew]ar}.report" /home/${User}/ftpuser/scanned/ || echo and so is this
        fi
    done
done
and you don't want to pass the password on the command line

Code:
cat > nolongersecret.sh <<EOF
for i in {1..20};do sleep 5;done
EOF
bash nolongersecret.sh ThisIsMyPasswd &
ps -ef | grep nolongersecret.sh
anyone can see it

Last edited by Firerat; 09-16-2013 at 09:39 AM. Reason: typ[os
 
1 members found this post helpful.
Old 09-16-2013, 09:54 AM   #6
dimitriauxio
LQ Newbie
 
Registered: Sep 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Ok I see what you have done. Well I can see you declare
Code:
 filename=/path/to/somefile
But This filename is the .config file that we want to find during the process of finding the ear and war files.
We do not know its name because it changes all the time depending on the ear or war filename.
For example if the ear file that we have found in the ftpuser directory is adc1.ear then it has to have a config file named adc.config,
which contains the particular information that we want to use later. (Packages)

What is your solution to this?
 
Old 09-16-2013, 10:03 AM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
well, in that case

you need to use find , to find your ear or war files
check for .config
then get package from .config
then do the rest....
 
1 members found this post helpful.
Old 09-16-2013, 10:06 AM   #8
dimitriauxio
LQ Newbie
 
Registered: Sep 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
I will try do something and I will post the code if I find any troubles.
Thanks for the moment to both of you!
 
Old 09-16-2013, 10:19 AM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,051

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
if your using scp you should use public key encryption instead of passwords.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash - passing associative arrays as arguments Lucien Lachance Programming 12 08-26-2013 01:43 PM
[SOLVED] BASH - passing arguments Garrett85 Programming 4 01-09-2013 04:03 PM
Passing arguments to shell script like perl script get-opts somupl86 Linux - Newbie 2 12-03-2010 12:14 AM
[SOLVED] passing commands as arguments to functions in bash script mastahyeti Linux - General 3 05-31-2010 07:19 PM
bash, passing arguments Four Linux - Newbie 3 02-06-2006 09:24 AM


All times are GMT -5. The time now is 04:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration