LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Does it possible to change path inside bash script? (https://www.linuxquestions.org/questions/programming-9/does-it-possible-to-change-path-inside-bash-script-4175586415/)

tomislav91 08-06-2016 03:46 AM

Does it possible to change path inside bash script?
 
i have some script with some paths inside it. The idea is to some files which is on desktop copy and move to another location. Problem is that inside script is similar to this:



Code:

cp test.zip /root/help/

because I allways have another zip files, does it possible to have some input which ask me for the name of file, and automaticly change in scripts?




Code:

./script
What is the zip name? test1.zip
.
.

And than it copy test1, not test. I tried to show you what I need, so please ask me if you need some other information.

Look example. I have zip file called test1..if open my script it wont work cause file to copy in script is test....so how to change that name or whole path inside script. Idea is to use that script inside pcs where is different zip files, and i dont want to change name allways inside scripr.

Michael Uplawski 08-06-2016 04:00 AM

You sure about the /root directory?

Code:

echo "file name?"
read FILENAME
echo "directory?"
read DIRECTORY
# echo $FILENAME
# echo $DIRECTORY

read bash shell builtin

grail 08-06-2016 04:55 AM

Not 100% sure I follow, but assuming I do, why not pass the name of the file to be copied to the script as a parameter and then use that in your copy command?

tomislav91 08-06-2016 08:59 AM

Code:

#!/bin/bash

echo -n "Give name to zip file and press [ENTER]: "
read var_name
var_dir="/root/Documents/test"
unzip -o $var_name -d  $var_dir
mv $var_dir/$var_name.c $var_dir/$var_name.d
service apache2 stop

Does this code seems ok? So in that zip file i have some file with some extension, I put .c (its example). So it copy, and change name, and restart service...
Can you check if code is right writed? It do what I want.

Can you just help me, if I want to inside that script to change some conf file. For example in /root/Documents/test.conf to delete some hash tag.

in my example of apache2 conf file, i want to DocumentRoot to be without that hashtag...

Code:

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        #DocumentRoot /var/www/


Michael Uplawski 08-06-2016 09:08 AM

Quote:

Originally Posted by grail (Post 5586852)
Not 100% sure I follow, but assuming I do, why not pass the name of the file to be copied to the script as a parameter and then use that in your copy command?

Your remark is valid. I assume, that the OP is about to elaborate a small script into something more complicated. Not knowing his actual requirements, I prefer to answer to his verbosely expressed need to be “asked for a file name”.

@tomislav91: grail has a point there. You can call your script with the archive as first parameter, like in
Code:

~$ script file-name
then analyze the parameter inside the script
Code:

FILENAME="$1"
# echo $FILENAME


Michael Uplawski 08-06-2016 09:11 AM

Quote:

Originally Posted by tomislav91 (Post 5586927)
It do what I want.

Then you're done.
Quote:

Code:

#DocumentRoot /var/www/

Explain first, why you need a script to uncomment a variable.

tomislav91 08-06-2016 09:21 AM

I need to do some task where default value is commended line. I don't need it. I need when my script do all work, to uncomment that line and restart some service. I think that my last post code is ok, just need this to do.

Michael Uplawski 08-06-2016 09:24 AM

Quote:

Originally Posted by tomislav91 (Post 5586943)
I need to do some task where default value is commended line. I don't need it. I need when my script do all work, to uncomment that line and restart some service. I think that my last post code is ok, just need this to do.

Why do you not uncomment it right away?

tomislav91 08-06-2016 09:31 AM

Why to do manualy? I just posted some example for apache conf, its not that, i just need to uncomment some line in some config file that I maked.

grail 08-06-2016 09:39 AM

So it would appear we have completely changed the initial requirement, ie. there is no longer a cp anywhere in the code.

While your snippet might work, my main objection would be that you never test if the user has supplied the name of an actual file and tested if you have access to it?
Code:

mv $var_dir/$var_name.c $var_dir/$var_name.d
I see a fair bit of issue with the above, assuming that the user has typed in the name of a zipped file (maybe test.zip), it is unlikely for there to exist a file called 'test.zip.c' and the fact that
you never test to see if there already exists a file called 'test.zip.d'.

As far as removing the hash, have a look at the sed command.

tomislav91 08-06-2016 10:31 AM

Can you give me a little help with sed command?

Yeah, but no worry about that, on Desktop it never will be some file with similar names, and you allways have access to it (if execute command with sudo).

grail 08-06-2016 10:58 AM

Show us your sed attempt and where you are stuck?

Here is a link to help :- http://www.grymoire.com/Unix/Sed.html

tomislav91 08-06-2016 02:42 PM

i did like this

Code:

sed 's/'#SOMETHING="none"'/'SOMETHING="none"'/' configfile.txt
but it not remove comment. why? I assume that problem is in character "
When I did it without, only lefting SOMETHING, than it did work. I tried to put /", but not work either.

grail 08-07-2016 02:26 AM

I am confused at why you have so many sets of quotes?? Would you please explain why all the single quotes are needed?

ondoho 08-07-2016 04:01 AM

Code:

sed 's/#SOMETHING="none"/SOMETHING="none"/' configfile.txt


All times are GMT -5. The time now is 03:41 AM.