PDA

View Full Version : [SOLVED] Bash Script Help Wanted



fallenshadow
March 7th, 2008, 06:13 PM
I recently have been using the command line for the first time ever. I have made many attempts to create a bash file that would copy a folder and its contents to /Opt.

What is the command to copy a folder and its contents to a directory such as /Opt?

Any help would really be appreciated.

ruy_lopez
March 7th, 2008, 06:21 PM
cp -r /folder /opt/

to copy /folder to /opt using "./script.sh /folder /opt"


cp -r $1 $2


You would typically add some checks for type, number of arguments etc.

fallenshadow
March 7th, 2008, 06:24 PM
Thanks for the quick reply, and what is the home folder in bash. I think it is $home but im not sure.

fallenshadow
March 7th, 2008, 06:25 PM
duplicate post

ruy_lopez
March 7th, 2008, 06:26 PM
It is $HOME

fallenshadow
March 7th, 2008, 06:50 PM
OK last question, I promise :) Is $HOME just home or is it home/username?

raijinsetsu
March 7th, 2008, 06:52 PM
$HOME should be the home directory of the user executing the scripts.
For example, if your username were RUPEY, $HOME would be "/home/RUPEY".

Why are you copying files to /OPT anyways?

fallenshadow
March 7th, 2008, 07:06 PM
I want to create an installer that would copy everything into /Opt as creating a .deb installer was too complicated. I have seen this type of installer before so I thought I would do the same.

Dr Small
March 7th, 2008, 07:09 PM
Just run:

echo $HOME

to see it's output.

mssever
March 8th, 2008, 01:19 AM
Note that Linux is case-sensitive. /Opt and /opt are entirely different directories. The first one doesn't exist unless you explicitly create it.

Also, any time you write a script to copy files (or use variables to store anything but numbers or fixed strings), be sure to surround the variables with double quotes.
cp -r "$1" "$2"If you omit the quotes, then Bad Things could happen if the variables contain special characters (including spaces).

Can+~
March 8th, 2008, 01:48 AM
If you need more things of bash:


env
(enviromental variables)

And to learn bash, there's a lot of pages out there, otherwise, you can use the manual

man bash
(a bit long, but you can search with "/keyword")

ghostdog74
March 8th, 2008, 05:00 AM
I recently have been using the command line for the first time ever. I have made many attempts to create a bash file that would copy a folder and its contents to /Opt.

What is the command to copy a folder and its contents to a directory such as /Opt?

Any help would really be appreciated.

you should go here (http://www.shelldorado.com/links/index.html#tutorials) first.

fallenshadow
March 8th, 2008, 12:56 PM
Thanks for the help everyone, this Ubuntu forum is so much more helpful than other forums. (such as LXF where they tell you to go read a manual or something). This is what I like, straight forward answers.

fallenshadow
March 8th, 2008, 08:59 PM
My script does work but you have to click run in terminal for it to work. I know this is because it is using sudo and not gksudo but when I try gksudo and click run it does not work. why?

mssever
March 8th, 2008, 10:13 PM
My script does work but you have to click run in terminal for it to work. I know this is because it is using sudo and not gksudo but when I try gksudo and click run it does not work. why?

I don't know if I'm understanding you properly, but unless it's a GUI script or a daemon, it can only run in a terminal, in almost all cases. Otherwise, where can it print what you tell it to print?

If I missed your question, please post some code. That'll make it much easier to answer.

fallenshadow
March 9th, 2008, 04:21 PM
You did answer my question... I have been trying a command like this to move a ".desktop" file into "/usr/share/applications":


sudo mv "$HOME/Desktop/folder/test a.desktop" "/usr/share/applications

but this will not work, does anyone know why?
I want to put a Gnome menu entry into the menu using bash.

ruy_lopez
March 9th, 2008, 04:33 PM
Try using $HOME like this:



mv "${HOME}/path/to/source/file" "/path/to/dest/"

mssever
March 9th, 2008, 04:46 PM
Try using $HOME like this:



mv "${HOME}/path/to/source/file" "/path/to/dest/"


The curly braces, while not harmful, won't make any difference in this situation.

@fallenshadow:
Are you running that command in a terminal? You should be. I can't see anything wrong with it, though.

What error message do you get? (and what exit status--available by looking at $? immediately after running the command in question)

ruy_lopez
March 9th, 2008, 04:55 PM
The curly braces, while not harmful, won't make any difference in this situation.


It's not just the curly braces I was showing. The paths have no spaces.

You're right, though. The braces are superfluous.

mssever
March 9th, 2008, 05:16 PM
It's not just the curly braces I was showing. The paths have no spaces.
Spaces in paths are OK as long as the path is quoted or the spaces are backslash-escaped.

I did notice a missing quote at the end of the code snippet fallenshadow posted--which could be the problem unless it's merely a copy-and-paste error.

ruy_lopez
March 9th, 2008, 05:30 PM
Also, if folder /usr/share/applications doesn't exist, moving a file there will create a file called applications.

Including a trailing slash "/use/share/applications/" will produce an error if the directory doesn't exist.

All this is assuming /usr/share/applications is supposed to be a directory. I couldn't tell from the quoted code.

mssever
March 9th, 2008, 06:01 PM
All this is assuming /usr/share/applications is supposed to be a directory. I couldn't tell from the quoted code.

/usr/share/applications is where .desktop files live.

ruy_lopez
March 9th, 2008, 06:12 PM
It's still good practice to use a trailing slash when moving files into directories, in case the directory doesn't exist.