LinuxQuestions.org
Review your favorite Linux distribution.
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 04-05-2017, 10:45 AM   #1
kerontide
LQ Newbie
 
Registered: Apr 2017
Posts: 12

Rep: Reputation: Disabled
Change work directory in a shell script doesn't works


Hi,

I develop my first time a little scrip shell for search a word inside of files on a folder, files can be in ascii in ebcdic can be compressed ... and works but only works if I execute the script inside the same folder who has the files.

So I decide to implement the change directory but doesn't works

is my first script so please don't be hard with me

Why when I execute the line:

cd $origin

I see with "pwd" command that I am in the correct path but script doesn't take care about the files of the origin path??

I hope I explain me well.

thanks a lot in advance.

Code:
#!/bin/bash
#
# Search.sh "search pattern;origin path;destiny result path(optional)" *
#
# Search.sh "46117;/home/sboixg/Descargas;/home/sboixg/Descargas/test" *  --> that means search 46117 into /home/sboixg/Descargas and copy the files who match with pattern into /home/sboixg/Descargas/test
#
# Search.sh "46117;/home/sboixg/Descargas;" * --> that means search 46117 into /home/sboixg/Descargas and print the result files
#
# Program has 4 parms:
# value you want to search 
# path origin where you want to search 
# path for copy the found records (optional)
# files filter for all *
#


parameters=$1 ; shift

workdir=$(dirname "$0")
echo "$workdir" - develop path

aux="$parameters"

IFS=';' read -a aux1 <<< "${aux}" #split ; in array

pattern=${aux1[0]}
origin=${aux1[1]}
aux2=${aux1[2]} 
dest=${aux2// }


listado=0
if [ -z "$dest" ]
then
    listado=1
fi

cd $origin
echo Confirm SEARCH IN the path BELOW - Y/N ?
pwd
read conf
if [ $conf != "Y" ]
then
    exit
fi

echo Working...please wait.
if [ $listado != 1 ] ; then
    if [ ! -d "$dest" ] ; then # check if folder destiny exist, if not create it.
      mkdir "$dest" 
    fi
fi

total=0
for x do
  isFile=$(file $x | cut -d\  -f2)
    if case "$isFile" in
            "gzip") < "$x" gzip -dc 2>/dev/null | dd conv=ascii 2>/dev/null | grep -q -e "$pattern" 2>/dev/null
                    if [ $? -ne 0 ] # capture the error
                     then 
                      < "$x" gzip -dc 2>/dev/null | grep -q -e "$pattern" 2>/dev/null
                    fi;;

        "ISO-8859") < "$x" dd conv=ascii 2>/dev/null | grep -q -e "$pattern" 2>/dev/null
                    if [ $? -ne 0 ]
                     then 
                      < "$x" grep -q -e "$pattern" 2>/dev/null
                    fi;;

            "data") < "$x" dd conv=ascii 2>/dev/null | grep -q -e "$pattern" 2>/dev/null
                    if [ $? -ne 0 ]
                     then 
                      < "$x" grep -q -e "$pattern" 2>/dev/null
                    fi;;

           "ASCII") < "$x" grep -q -e "$pattern" 2>/dev/null
                    if [ $? -ne 0 ]
                     then 
                      < "$x" grep -q -e "$pattern" 2>/dev/null
                    fi;;

                 *) < "$x" grep -q -e "$pattern" 2>/dev/null;;
        esac
    then
        total=$((total+1))
        if [ $listado = 1 ]
         then
            echo "$x"
         else
            cp "$x" "$dest" 
        fi
    fi
done

if [ $listado != 1 ] ; then
    echo "$total" files copied to the destiny path "$dest" who contain pattern "$pattern" 
    ls -l "$dest"
fi
exit 1

Last edited by kerontide; 04-05-2017 at 11:24 AM.
 
Old 04-05-2017, 11:02 AM   #2
kerontide
LQ Newbie
 
Registered: Apr 2017
Posts: 12

Original Poster
Rep: Reputation: Disabled
I will try to clarify my problem

I have huge volume of files in path A (I have a thousand of paths with thousand of files) and I want that my script (that seems works) stay in a folder like /Tools and who need to search a file with a value inside can use it.

then my script has these parameters:

1. word you are looking for
2. origin path (where the files are stored)
3. destiny path (optional parameter if doesn't comes list the files founded if comes copy there the files) [works fine]

problem is if I execute the script in /Tools and I set the parameters like this:

kerontide@cinnamon /Tools $ Search.sh "3345667;/bin/arch/A;" *

script take the files for search of the folder /Tools instead the folder /bin/arch/A

this is why I ask why my "cd" command doesn't works
 
Old 04-05-2017, 11:19 AM   #3
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,762

Rep: Reputation: 684Reputation: 684Reputation: 684Reputation: 684Reputation: 684Reputation: 684
dont use ';' as a sepetrator and remove the IFS ... code, if your params include spaces then use quotes, also please use [code] ... [/code]tags around your code to make it readable, see:
http://www.linuxquestions.org/questi....php?do=bbcode
 
Old 04-05-2017, 11:25 AM   #4
kerontide
LQ Newbie
 
Registered: Apr 2017
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Keith Hedger View Post
dont use ';' as a sepetrator and remove the IFS ... code, if your params include spaces then use quotes, also please use [code] ... [/code]tags around your code to make it readable, see:
http://www.linuxquestions.org/questi....php?do=bbcode
thanks about the code, now is ok,

about your suggestion...why I can't not use ";" ? it works fine, do you think is related to my problem? if I use other separator will works fine?

thanks.
 
Old 04-05-2017, 11:50 AM   #5
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,762

Rep: Reputation: 684Reputation: 684Reputation: 684Reputation: 684Reputation: 684Reputation: 684
basically if you dont have any pressing need to change the default separator (space) then dont, especially when you are first learning scripting you will just cause your self problems as changing IFS can cause subtle problems that are difficult to diagnose, but simply why cause yourself more problems than you need to.
 
Old 04-05-2017, 12:30 PM   #6
kerontide
LQ Newbie
 
Registered: Apr 2017
Posts: 12

Original Poster
Rep: Reputation: Disabled
I use IFS for split the parameters, because I want to use as optional the third parameter.

Which is the command that I can use for split values into an array for evaluate them?

Can I evaluate a parm null?

Thanks in advance
 
Old 04-05-2017, 01:43 PM   #7
kerontide
LQ Newbie
 
Registered: Apr 2017
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Keith Hedger View Post
basically if you dont have any pressing need to change the default separator (space) then dont, especially when you are first learning scripting you will just cause your self problems as changing IFS can cause subtle problems that are difficult to diagnose, but simply why cause yourself more problems than you need to.
I continue without know why I can use "cd" inside my script and if I use what you comment doesn't work also. Now without the "splited paramter" works more strange.

for example I delete the IFS and use the "natural" parameters

Code:
#!/bin/bash
#
# ./Search.sh "search pattern;origin path;destiny result path(optional)" *
#
# ./Search.sh "46117;/home/sboixg/Descargas;/home/sboixg/Descargas/test" *  --> that means search 46117 into /home/sboixg/Descargas and copy the files who match with pattern into /home/sboixg/Descargas/test
#
# ./Search.sh "46117;/home/sboixg/Descargas;" * --> that means search 46117 into /home/sboixg/Descargas and print the result files
#
# Program has 4 parms:
# value you want to search 
# path origin where you want to search 
# path for copy the found records (optional)
# files filter for all *
#

#parameters=$1 ; shift

pattern=$1
origin=$2
dest=$3 ; shift


workdir=$(dirname "$0")
echo "$workdir" desarrollo


listado=0
if [ -z "$dest" ]
then
    listado=1
fi


printf '%s\n' "$listado"
printf '%s\n' "$pattern"
printf '%s\n' "$dest"
printf '%s\n' "$origin"
doesn't works, when I print $dest and I let this parm in blank return a filename in the folder and also I set other folder as origin and doesn't works, well works worst

Code:
kerontide@cinnamon ~/Descargas $ bash Search.sh NAD\+CZ\+000916800 /home/kerontide/Descargas/test *
. desarrollo
0
NAD+CZ+000916800
0011823513.9b18e780-b024-11e5-a5c2-27140a1468a0.received
/home/sboixg/Descargas/test
Confirm SEARCH IN the path BELOW - Y/N ?
$pattern = NAD+CZ+000916800
$dest=0011823513.9b18e780-b024-11e5-a5c2-27140a1468a0.received ? I don't fill this parameter is in blank, is this cause by the "*"?
$origin=/home/sboixg/Descargas/test

Last edited by kerontide; 04-05-2017 at 01:47 PM.
 
Old 04-05-2017, 02:16 PM   #8
kerontide
LQ Newbie
 
Registered: Apr 2017
Posts: 12

Original Poster
Rep: Reputation: Disabled
finally I use the "natural" parameters but I don't understand why if I only put 2 parm system answer me with the first file name, this is the most strange

but finally I think I use it well checking this third parameter:
Code:
#!/bin/bash

pattern=$1
origin=$2
dest=$3 

aux2="$dest"
dest=${aux2// }

workdir=$(dirname "$0")
echo "$workdir" - develop path


listado=0
if [ -z "$dest" ]
then
    listado=1
fi

m=`expr substr "$dest" 1 1`
if [ $m != "/" ]
then
    listado=1
fi
but I still have the problem when I run the script for check files inside other path different of the script path, the pwd return script is in the correct "origin" path but when run they start to list the files on the script path, why this behavior?

thanks a lot in advance.
 
Old 04-05-2017, 02:51 PM   #9
kerontide
LQ Newbie
 
Registered: Apr 2017
Posts: 12

Original Poster
Rep: Reputation: Disabled
I think I have it, it seems my error was only put * at the end, it seems is necessary to put the entire path

and works with IFS also

thanks a lot!!

I will put here the last version of the code in few minutes.
 
Old 04-05-2017, 03:45 PM   #10
kerontide
LQ Newbie
 
Registered: Apr 2017
Posts: 12

Original Poster
Rep: Reputation: Disabled
final code that works perfect!!!! thanks a lot for your help:

Code:
#!/bin/bash
#
# ./Search.sh "search pattern;destiny result path(optional)" *
#
# Program has 3 parms:
# value you want to search 
# path for copy the found records (optional)
# path origin where you want to search with * for all or *.gz or...
#

#get parameters
parameters=$1
workdir=$(dirname "$2")

#set working directory
origin="$workdir"

#split parameter $1 into array for check values pattern & optional destiny path
aux="$parameters"
IFS=';' read -a aux1 <<< "${aux}" #split ; in array

pattern=${aux1[0]}
aux2=${aux1[1]} 
dest=${aux2// } #trim blank chars


listado=0
if [ -z "$dest" ] #if $dest is null means you want list the files only
then
    listado=1
fi

#check the search origin path
cd $origin
echo 'Confirm SEARCH IN the path --> '"$origin" '(Y/N) ?' 
read conf
if [ $conf != "Y" ]
then
    exit
fi

echo Working...please wait.
if [ $listado != 1 ] ; then
    if [ ! -d "$dest" ] ; then # check if folder destiny exist, if not create it.
      mkdir "$dest" 
    fi
fi

total=0
for x do
  isFile=$(file $x | cut -d\  -f2)
  #printf '%s\n' "$isFile"
  #printf '%s\n' "$x"
    if case "$isFile" in
          "cannot") != "cannot" 2>/dev/null;; #first movement the pattern, don't take care

            "gzip") < "$x" gzip -dc 2>/dev/null | dd conv=ascii 2>/dev/null | grep -q -e "$pattern" 2>/dev/null
                    if [ $? -ne 0 ] # capture the error
                     then 
                      < "$x" gzip -dc 2>/dev/null | grep -q -e "$pattern" 2>/dev/null
                    fi;;

        "ISO-8859") < "$x" dd conv=ascii 2>/dev/null | grep -q -e "$pattern" 2>/dev/null
                    if [ $? -ne 0 ]
                     then 
                      < "$x" grep -q -e "$pattern" 2>/dev/null
                    fi;;

            "data") < "$x" dd conv=ascii 2>/dev/null | grep -q -e "$pattern" 2>/dev/null
                    if [ $? -ne 0 ]
                     then 
                      < "$x" grep -q -e "$pattern" 2>/dev/null
                    fi;;

           "ASCII") < "$x" grep -q -e "$pattern" 2>/dev/null
                    if [ $? -ne 0 ]
                     then 
                      < "$x" grep -q -e "$pattern" 2>/dev/null
                    fi;;

                 *) < "$x" grep -q -e "$pattern" 2>/dev/null;;
        esac
    then
        total=$((total+1))
        if [ $listado = 1 ]
         then
            echo "$x"
         else
            cp "$x" "$dest" 
        fi
    fi
done

if [ $listado != 1 ] ; then
    echo "$total" files copied to the destiny path "$dest" who contain pattern "$pattern" 
    ls -l "$dest"
fi
exit 1
 
Old 04-06-2017, 04:05 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,699

Rep: Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494Reputation: 2494
Re
Code:
if [ $listado != 1 ] ; then
http://tldp.org/LDP/abs/html/comparison-ops.html; use '-ne'.

I also recommend [[ ]] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
 
Old 04-06-2017, 05:04 AM   #12
kerontide
LQ Newbie
 
Registered: Apr 2017
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
Re
Code:
if [ $listado != 1 ] ; then
http://tldp.org/LDP/abs/html/comparison-ops.html; use '-ne'.

I also recommend [[ ]] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
thanks a lot!!
 
  


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
How to change directory in a shell script? jdupre Linux - Newbie 13 06-25-2015 06:03 AM
[SOLVED] Shell script doesn't run on cron (otherwise it works) kalashari Linux - Software 6 02-17-2012 02:50 PM
Shell script doesn't think a directory is valid (but it is) ssrobins Linux - General 4 04-21-2010 10:23 PM
Shell script to start MPlayer doesn't work in any other directory than /root lagu2653 Programming 5 04-28-2006 04:22 AM
A C-shell script doesn't work on redhat 7.3 but works on Sun Solaris Belmer Linux - Newbie 2 02-08-2004 12:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:23 PM.

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