LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-28-2015, 11:35 PM   #1
schandran
LQ Newbie
 
Registered: Apr 2014
Posts: 9

Rep: Reputation: Disabled
Bash scripting help


Hello All,

I'm new to bash scripting and am running into a problem. The following is my script:

directory=$HOME
filename=$#
#echo -n "Enter the filename that you would like to test: "
#read filename
if test $# -eq 0
then
echo "You have entered no parameters" > find-name.error
exit 1
fi
if test $# -gt 1
then
echo "You have entered more than one parameters" > find-name.error
exit 1
fi
if [[ ! -z `find $HOME -name $filename` ]]
then
cp $filename $HOME/scripts
echo "$filename has been copied to your home directory " > find-name.result
else
echo "The $filename you are looking for cannot be found" > find-name.error
fi
exit 1

The tests all work fine, but the find command reads the input as "1" instead of the filename I give as input. The proper syntax is " scriptname filename" . How do I get the script to read the input without using a read statement?
 
Old 03-28-2015, 11:52 PM   #2
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
The correct script is:

Code:
directory=$HOME
filename="$1"

#echo -n "Enter the filename that you would like to test: "
#read filename

if test $# -eq 0 
 then
  echo "You have entered no parameters" > find-name.error
  exit 1
fi

if test $# -gt 1 
 then
  echo "You have entered more than one parameters" > find-name.error
  exit 1
fi

if [[ ! -z "`find $HOME -name "$filename"`" ]]
 then
  cp "$filename" $HOME/scripts 
  echo "$filename has been copied to your home directory " > find-name.result
  exit 0
else
 echo "The $filename you are looking for cannot be found" > find-name.error 
fi

exit 1

Last edited by veerain; 03-28-2015 at 11:53 PM.
 
Old 03-29-2015, 12:27 AM   #3
schandran
LQ Newbie
 
Registered: Apr 2014
Posts: 9

Original Poster
Rep: Reputation: Disabled
Thank You Veera, that works! I didn't think to assign file name as $1 since I was using $# for the tests.
 
Old 03-29-2015, 12:39 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
I see some issues with the logic around which file you are copying.
Code:
find $HOME -name "$filename"
The above will look below the users home directory for any file matching that stored in 'filename' variable.

Assuming the above returns true you submit the following:
Code:
cp "$filename" $HOME/scripts
Now, unlike the find command, this command shows the 'filename' supplied will be in the current directory (which I might add does not have to be within the HOME directory).

To provide an example and assume we are running from the HOME directory:
Code:
HOME-file1
    -file2
    -dir1-filename
    -scripts
With the above structure, the find would return that it found your filename value but the cp would error as there is no filename in the HOME directory to copy.

Also, have you considered the case where there are multiple files under different directories all with the same name? Again, your find will say it found at least one (in fact all), but again
you have no confidence it will be the one in the top level HOME directory, assuming there is one there at all.

Final note, you create a 'directory' variable but never use it
 
Old 03-29-2015, 12:58 AM   #5
schandran
LQ Newbie
 
Registered: Apr 2014
Posts: 9

Original Poster
Rep: Reputation: Disabled
Hi Grail,

You are correct in that the cp command failed. At the time I didn't use the full path as I was trying to figure out why the find command wasn't working. I used $HOME to make it easier to troubleshoot. The final cp command looked like this:

PHP Code:
   if [[ ! -"`find $directory -name "$filename"`" ]]
          
then
            cp 
"$directory/$filename$HOME
            
echo "The file $filename has been copied to your home directory " find-name.result
          
else
            echo 
"The file $filename you are looking for cannot be found" find-name.error
        fi 
 
Old 03-29-2015, 02:53 AM   #6
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
This still supports copying file from $HOME directory only, not from it's subdirectory. You can use find's -exec option to run a script to do the correct job.
 
Old 03-29-2015, 05:58 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
As veerain has mentioned, all you have done here is obscure HOME by using directory. The cp command is still only looking directly under whatever 'directory' is set to for the copy from portion,
so if your 'filename' were at the following location :- $directory/somwhere_else/filename, your cp command will again fail.

As per my previous post, you are also not taking into account there may be more than 1.
 
  


Reply



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 Scripting – Code Structure - Defining Multiple Points Of Entry In Bash Script carlr Programming 10 08-25-2014 02:38 AM
[To share Bash knowledge]Notes for Advanced Bash-Scripting Version 10 (Latest) jcky Programming 4 07-31-2014 09:24 AM
LXer: Bash If statements, Exit Status and Comparison Operators, A beginners guide to bash scripting LXer Syndicated Linux News 0 06-29-2014 07:35 PM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
Bash Scripting help jgtg32a Programming 5 09-06-2005 09:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:53 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
Open Source Consulting | Domain Registration