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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
04-05-2017, 10:45 AM
|
#1
|
LQ Newbie
Registered: Apr 2017
Posts: 12
Rep:
|
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.
|
|
|
04-05-2017, 11:02 AM
|
#2
|
LQ Newbie
Registered: Apr 2017
Posts: 12
Original Poster
Rep:
|
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
|
|
|
04-05-2017, 11:19 AM
|
#3
|
Senior Member
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,205
|
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
|
|
|
04-05-2017, 11:25 AM
|
#4
|
LQ Newbie
Registered: Apr 2017
Posts: 12
Original Poster
Rep:
|
Quote:
Originally Posted by Keith Hedger
|
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.
|
|
|
04-05-2017, 11:50 AM
|
#5
|
Senior Member
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,205
|
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.
|
|
|
04-05-2017, 12:30 PM
|
#6
|
LQ Newbie
Registered: Apr 2017
Posts: 12
Original Poster
Rep:
|
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
|
|
|
04-05-2017, 01:43 PM
|
#7
|
LQ Newbie
Registered: Apr 2017
Posts: 12
Original Poster
Rep:
|
Quote:
Originally Posted by Keith Hedger
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.
|
|
|
04-05-2017, 02:16 PM
|
#8
|
LQ Newbie
Registered: Apr 2017
Posts: 12
Original Poster
Rep:
|
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.
|
|
|
04-05-2017, 02:51 PM
|
#9
|
LQ Newbie
Registered: Apr 2017
Posts: 12
Original Poster
Rep:
|
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.
|
|
|
04-05-2017, 03:45 PM
|
#10
|
LQ Newbie
Registered: Apr 2017
Posts: 12
Original Poster
Rep:
|
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
|
|
|
04-06-2017, 04:05 AM
|
#11
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,420
|
|
|
|
04-06-2017, 05:04 AM
|
#12
|
LQ Newbie
Registered: Apr 2017
Posts: 12
Original Poster
Rep:
|
Quote:
Originally Posted by chrism01
|
thanks a lot!!
|
|
|
All times are GMT -5. The time now is 11:34 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|