LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Homework questions (https://www.linuxquestions.org/questions/linux-newbie-8/homework-questions-892616/)

isha 07-19-2011 08:55 PM

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

Tinkster 07-19-2011 08:56 PM

Hi, welcome to LQ!

And here's a good place.


Cheers,
Tink

isha 07-19-2011 09:58 PM

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

Tinkster 07-19-2011 10:05 PM

Quote:

Originally Posted by isha (Post 4419690)
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

isha 07-19-2011 10:16 PM

What do you mean by code tags?,. I put left the space now I am getting.
" syntax error near unexpected token 'fi'

flamelord 07-19-2011 10:17 PM

this probably isn't related to the problem you posted, but I think you want to use "\{\}" instead of "*.jpg" in your exec section.

isha 07-19-2011 10:25 PM

I made that change in the -exec section "\{\}" instead of "*.jpg" but that did not stop th "fi" error

flamelord 07-19-2011 10:53 PM

I think you need a newline between elif and the echo command, that should fix your fi problem.

David Triebwasser 07-19-2011 10:54 PM

You've got mismatched bracket/braces here

if ["$tipo"==jpg};

I leave the answer up to you, but you're close...

ScottSmith 07-19-2011 11:07 PM

Quote:

Originally Posted by isha (Post 4419690)

if ["$tipo"==jpg};


You need to use square braces instead of curly braces.

Late post, needed to reload the page.

Tinkster 07-19-2011 11:27 PM

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


chrism01 07-19-2011 11:53 PM

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.

grail 07-20-2011 02:52 AM

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

MTK358 07-20-2011 06:53 AM

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.

MTK358 07-20-2011 07:04 AM

Quote:

Originally Posted by isha (Post 4419690)
Code:

DATE=`date +%d-%m-%y`

Again, use $(command) (see my previous post).

Quote:

Originally Posted by isha (Post 4419690)
Code:

dest= /home/me/homework/jpgfil

There may not be spaces around the "=" sign.

Quote:

Originally Posted by isha (Post 4419690)
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 (Post 4419690)
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 (Post 4419690)
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 (Post 4419690)
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.


All times are GMT -5. The time now is 06:38 AM.