0
#!/bin/bash    

#become root
UID=$(id -u)
if [ x$UID != x0 ] 
then
    printf -v cmd_str '%q ' "$0" "$@"
    exec sudo su -c "$cmd_str"
fi


mkdir ~/.D3GO/; 
cp -a `pwd`/viewright_backup/. ~/.D3GO/;

mkdir /opt/D3GO/;
cp `pwd`/D3GO /opt/D3GO/;
cp `pwd`/D3GO.png /opt/D3GO/;
cp `pwd`/D3GO.desktop /usr/share/applications/;
chmod +x /opt/D3GO/D3GO;

As you can see, because this uses ~, and the script is ran as root, it creats the folder.D3GO in the /root/ directory. Is it possible to create it in the home directory. For example, if the script is located in /home/user/Downloads/, it should create the directory in home/user/. If it's located in /home/user2/Downloads/dir/, it should create it in /home/user2/ etc. Is this possible? Thanks!

Maybe something like:

#!/bin/bash    

#remember the username
user = $(whoami);

#become root
UID=$(id -u)
if [ x$UID != x0 ] 
then
    printf -v cmd_str '%q ' "$0" "$@"
    exec sudo su -c "$cmd_str"
fi


mkdir /home/`echo user`/.D3GO/; 
cp -a `pwd`/viewright_backup/. /home/`echo user`/.D3GO/;
  • Don't become root, use sudo for each line, and explicitly specify /root instead of ~. – saiarcot895 Jul 8 '14 at 21:13
  • No, no, I'm not trying to create ./D3GO in /root/. – Dusan Milosevic Jul 8 '14 at 21:15
  • 2
    Why do you insist in using /home/? Toss those files in /opt/D3GO/ or /usr/local/(bin/). – Rinzwind Jul 8 '14 at 21:15
  • @DusanMilosevic: My bad. My brain twisted something. – saiarcot895 Jul 8 '14 at 21:16
1

You are making this more difficult then it needs to be.

First, there is no reason your script (as posted) needs to run as root.

Second, you are using /opt rather then /usr/local . By default , ubuntu uses /usr/local and /usr/local is on the default path.

And third your are using ambiguous locations such as .. and pwd and ~ . It is best to use the full path and standard locations.

Why not use $HOME ?

[[ -d "$HOME"/.D3GO ]] || mkdir "$HOME"/.D3GO

Most of your other problems are solved with a decent make file or a well packaged .deb . If your script is dependent on things outside the script, files or .png, package it into a .deb or write a make file so that the things you need are installed into standard locations /usr/local or in the system if you wish.

If you are installing something, you would provide an archive and you should write your script relative to the archive you provide.

So your archive might be FOO and inside you would have

FOO/files FOO/img FOO/Your_script

So "Your_Script" can just call "cp ./file/file_to_install /where/to/install"

To be honest, most of your installation ("cp pwd" stuff) is best handled with either

  1. Proper packaging into a .deb

  2. A make file .

Either of the above have numerous advantages including, but not limited to, ease of installation, ease of removal, and installing files to standard, known locations.

  • The problem is that I don't know anything about deb files, and my second code looks ok to me at least. I know a little about scripts. So, I'm trying to "install" this script. – Dusan Milosevic Jul 8 '14 at 21:23
  • Well, again , your lack of knowledge is half your problem. Rather then using standard locations and install techniques you are re-inventing the wheel without really understanding what a wheel should look like. I highly suggest you use Standard locations ( /usr/local ) and at a minimum start with an archive with a known starting layout. Trying to move $random_files from $random_locations and ending up in /root should all suggest you are off on the wrong foot. – Panther Jul 8 '14 at 21:26
  • sorry it that came across as a bit harsh, that was not my intention. I outlined a basic archive structure that would greatly simplify your script. The use of standard locations , keeping files on the $PATH and standard locations also helps. No need to copy shared files into each user $HOME , just put them in /usr/local/bin or what not. – Panther Jul 8 '14 at 21:34
  • No, no, I didn't get offended. I really meant "Thank you!" for giving me an advice. I always listen to advices especially if they come from someone experienced. – Dusan Milosevic Jul 8 '14 at 21:44
  • 1
    The problem is that, I cannot use the standard locations because these scripts should fix the problem with file that is located in ~/.wine-pipelight/ and I'm trying to make a file that will help other people in my country, because the most famous TV provider won't support linux with D3GO(the TV via internet) etc. Long story... Maybe I simply doesn't have the knowledge to make it work for others... I managed to make it work,but it's hard to install and set up :( – Dusan Milosevic Jul 8 '14 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.