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 07-19-2011, 08:55 PM   #1
isha
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Rep: Reputation: Disabled
Homework questions


I am writing a script from an assignment in my Linux class and I am having problems. Where can I post my script to get suggestions about what could be wrong
 
Old 07-19-2011, 08:56 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, welcome to LQ!

And here's a good place.


Cheers,
Tink
 
Old 07-19-2011, 09:58 PM   #3
isha
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
find + -exec + if

so my homework is to find the jpg files in a folder specified by the user and move them to another folder with the date added to the filename. I have not gotten to the name change part. I need to fix this first.

this is my script and I am having an error " -exec missing argument"

#!/bin/bash
DATE=`date +%d-%m-%y`
dest= /home/me/homework/jpgfil

read -p " what directory do you want to search?" dir
echo " Listing directory content"
ls $dir
read -p "what type of file are you looking for?" tipo (this is just an extra question, it is not needed because he wants the script only for jpg files)

if ["$tipo"==jpg};
then
find $dir -name "*.jpg" -exec mv *.jpg $dest\;
elif echo "Extension not supported"
fi
 
Old 07-19-2011, 10:05 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by isha View Post
so my homework is to find the jpg files in a folder specified by the user and move them to another folder with the date added to the filename. I have not gotten to the name change part. I need to fix this first.

this is my script and I am having an error " -exec missing argument"

#!/bin/bash
DATE=`date +%d-%m-%y`
dest= /home/me/homework/jpgfil

read -p " what directory do you want to search?" dir
echo " Listing directory content"
ls $dir
read -p "what type of file are you looking for?" tipo (this is just an extra question, it is not needed because he wants the script only for jpg files)

if ["$tipo"==jpg};
then
find $dir -name "*.jpg" -exec mv *.jpg $dest\;
elif echo "Extension not supported"
fi


The problem is the missing space between $dest and \;


May I suggest that you put code tags around your code?



Cheers,
Tink
 
Old 07-19-2011, 10:16 PM   #5
isha
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
What do you mean by code tags?,. I put left the space now I am getting.
" syntax error near unexpected token 'fi'
 
Old 07-19-2011, 10:17 PM   #6
flamelord
Member
 
Registered: Jun 2011
Distribution: Arch Linux
Posts: 151

Rep: Reputation: 34
this probably isn't related to the problem you posted, but I think you want to use "\{\}" instead of "*.jpg" in your exec section.
 
Old 07-19-2011, 10:25 PM   #7
isha
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
I made that change in the -exec section "\{\}" instead of "*.jpg" but that did not stop th "fi" error
 
Old 07-19-2011, 10:53 PM   #8
flamelord
Member
 
Registered: Jun 2011
Distribution: Arch Linux
Posts: 151

Rep: Reputation: 34
I think you need a newline between elif and the echo command, that should fix your fi problem.
 
Old 07-19-2011, 10:54 PM   #9
David Triebwasser
LQ Newbie
 
Registered: Jul 2011
Location: Richmond, CA
Posts: 7
Blog Entries: 1

Rep: Reputation: Disabled
You've got mismatched bracket/braces here

if ["$tipo"==jpg};

I leave the answer up to you, but you're close...
 
Old 07-19-2011, 11:07 PM   #10
ScottSmith
LQ Newbie
 
Registered: Mar 2004
Distribution: Debian - Sid
Posts: 23

Rep: Reputation: 1
Quote:
Originally Posted by isha View Post

if ["$tipo"==jpg};

You need to use square braces instead of curly braces.

Late post, needed to reload the page.

Last edited by ScottSmith; 07-19-2011 at 11:09 PM.
 
Old 07-19-2011, 11:27 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
What do you mean by code tags?,.
Code tags make code easier to read by maintaining
proper formatting. To get code tags you put this
PHP Code:
[CODE][/CODE
around the piece of code. See the effect below.

Quote:
I put left the space now I am getting.
" syntax error near unexpected token 'fi'
Sorry, no idea what "I put left the space" implies.

Code:
#!/bin/bash
DATE=`date +%d-%m-%y`
dest= /home/me/homework/jpgfil

read -p " what directory do you want to search?" dir
echo " Listing directory content"
ls $dir
read -p "what type of file are you looking for?" tipo #(this is just an extra question, it is not needed because he wants the script only for jpg files)

if ["$tipo"==jpg};
then
  # your line
  find $dir -name "*.jpg" -exec mv *.jpg $dest\;
  # correct line
  find $dir -name "*.jpg" -exec mv *.jpg $dest \;
elif 
  echo "Extension not supported"
fi

Last edited by Tinkster; 07-19-2011 at 11:41 PM.
 
Old 07-19-2011, 11:53 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,286

Rep: Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347Reputation: 2347
Lots of good tips there; here's some good links to bash programming tutorials/docs
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

One thing I'd recommend for debugging is
Code:
set -xv
as the 2nd line of your script. It shows you exactly what bash is doing/trying to do in detail.
 
Old 07-20-2011, 02:52 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
I would add that I am surprised your script gets as far as it does before presenting an error as the following:
Code:
dest= /home/me/homework/jpgfil
This would cause the very first error. Like:
Code:
./script.sh: line 3: /home/me/homework/jpgfil: is a directory
 
Old 07-20-2011, 06:53 AM   #14
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Also, don't use backticks, use $(command) instead. It nests easily and can't be confused with commas.

It only works with bash (not plain sh), but I see that you're using bash anyway.
 
Old 07-20-2011, 07:04 AM   #15
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by isha View Post
Code:
DATE=`date +%d-%m-%y`
Again, use $(command) (see my previous post).

Quote:
Originally Posted by isha View Post
Code:
dest= /home/me/homework/jpgfil
There may not be spaces around the "=" sign.

Quote:
Originally Posted by isha View Post
Code:
read -p " what directory do you want to search?" dir
echo " Listing directory content"
read -p "what type of file are you looking for?" tipo (this is just an extra question, it is not needed because he wants the script only for jpg files)
Why are you putting spaces in front of the strings you're printing? And if they're intentional, why is there not one in the last line in the above snippet?

Quote:
Originally Posted by isha View Post
Code:
ls $dir
Always put quotes around variables that contain filenames, since otherwise it will be split into separate arguments if the filename has spaces.

Quote:
Originally Posted by isha View Post
Code:
if ["$tipo"==jpg};
then
The "[" syntax is not some kind of expression, it's a command. That means that there have to be spaces between the arguments. Also, it should be a square bracket at the end, and the semicolon is not needed since there already is a newline before the "then".

http://mywiki.wooledge.org/BashPitfa...22.24foo.22.5D

Quote:
Originally Posted by isha View Post
Code:
find $dir -name "*.jpg" -exec mv *.jpg $dest\;
Again, put quotes around variables that contain filenames. Anyway, there should be a space between "$dest" and "\;".

Also, what is will do is attempt to copy all "*.jpg" files in the current directory into "$dest" every time "find" comes across a matching file. I don't think that's what you want. Even if that's what you want, if a filename you want to copy happens to contain "{}", find will replace it with the current filename that it found.
 
  


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




All times are GMT -5. The time now is 06:06 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