Is it possible to change paths inside a 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:

cp test1.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?

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

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

Not sure I understand your problem correctly. Did you consider the bash builtins read or select ?

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.

If your script ALWAYS needs to have a zip file, why not just pass the name of that file to your script as an operand:

./script file.zip

and change the contents of script to:

cp "$1" /root/help/

Otherwise, assuming that you are using bash as your shell, why not look at the man page for bash on your system and see if the read or select built-ins would help you do what you want (as RudiC suggested)? If you are not using bash as your shell, look at the man page for the shell you are using for the description of the read built-in in whatever shell you are using.

I did my bash like this

echo -n "give a name for script [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 
sed 's/#SOMETHING="none"/SOMETHING="none"/' configfile.txt 
service something restart

I did in your way Don, but when type a name of script, I used .zip extension. Should I, or just give a name? Why am I asking. Because when I wrote like this

var_dir="/root/Documents/test"
unzip -o "$1" -d  $var_dir
mv $var_dir/"$1".c $var_dir/"$1".d

script don't work, cause he can't move file because there is no a file like example_of_file.ZIP.C . Can you get a point? If I mv a file and add just a argument, he took and extension, but i don't want to, just a name.

It looks like you now have a working script. Here are a few comments that you may find useful in future projects:

  1. Your earlier posts talked about copying a single file; not about unzipping a file, copying another file, and editing another file. While it is true that unzip will accept a pathname with or without the .zip extension, cp (which was in your earlier posts), mv , sed , ed , and ex require a complete filename. There are easy ways to strip off the .zip from a pathname supplied by the user using variable expansions. Using that we can let the user give the filename of the ZIP file to be processed with or without the .zip extension.
  2. The meaning of echo -n varies from implementation to implementation and in bash depends on what options are specified when bash is started. It is easier to write portable scripts if you use printf instead of echo if the first argument to echo starts with a hyphen and if any argument to echo contains a backslash character.
  3. Anytime you get a pathname (or any string) from a user, you should verify that a non-empty string was supplied and be prepared for the possibility that that string might contain whitespace characters. (Note that even though having spaces and tabs in filenames is legal, scripts like your fail if it tries to process those names because the references to the strings provided by the user are not quoted.)
  4. From a human factors standpoint, the prompt give a name for script [ENTER]: sounds like you are asking for the name of a script to be executed instead of the name of a ZIP file to be processed.
  5. You should verify that attempts to execute a command succeeded before proceeding. (For example, assuming that the ZIP file named by the user exists and the unzip command worked before trying to copy a file from a shared directory where files will be unzipped

Also note that the command:

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

copies the contents of that configuration file to the user's terminal with the possible removal of an octothorp from some lines in the file; but it makes absolutely no changes to the contents of that file.

Consider the following possible alternative for your script (which changes the contents of configfile.txt instead of just displaying a possibly changed version of that file on the users selected output file:

#!/bin/bash
IAm=${0##*/}	# Save last component of script name for diagnostics

var_dir="/root/Documents/test"
cd "$var_dir" || exit 1	# Exit if we can't get into our target directory

# Prompt for, get, and verify ZIP filename...
printf 'Enter name of file to be unzipped (with or without .zip extension): '
read var_name 
var_base=${var_name%.zip}
if [ ! -r "$var_base".zip ]
then	printf '%s: %s not found or not readable.' "$IAm" "$var_base".zip >&2
	exit 2
fi

# Unzip the named file into the current directory and move the file extracted
# from that archive with the extension ".c" in place of ".zip" for the archive
# to have the extension ".d" instead of ".c".
unzip -o "$var_base".zip || exit 3
mv "$var_base".c "$var_base".d || exit 4

ed -s configfile.txt <<-"EOF" || exit 5
	g/#SOMETHING="none"/s//SOMETHING="none"/
	w
	1,$p
	q
EOF
service something restart

Note that some implementations of sed allow in-place editing of files (like I am doing with ed in the above script), but that option is not portable (and even if it is available on your system, many consider using it dangerous). I am only using features specified by the POSIX standards in this script (other than the absolute pathname for your shell at the start of the script) since you have not specified what operating system you're using. There are some shortcuts that could be used in bash , but I have avoided using them to make your script more portable.

I have told ed to not only update the file, but to also copy the entire contents of the updated file to standard output (to come closer to matching the output your script produced). If you don't need (or want) to see the contents of the updated file, remove the 1,$p line shown in red from the ed commands in the here-document in the script.

By changing directory to $var_dir before the unzip , we get rid of the need for the unzip -d option and speed up the unzip , mv , and ed commands slightly.

1 Like

i tried your code. When I enter zip name, i god message that file is not found or not readable..

Please show us the output from the command:

ls -l /root/Documents/test

Then show us the exact output you get when you use the following command to run your script:

bash -xv script_name

where script_name is an absolute pathname of the file containing your script.

sudo bash -xv script
#!/bin/bash
IAm=${0##*/}	# Save last component of script name for diagnostics
+ IAm=script

var_dir="/root/Documents/test"
+ var_dir=/root/Documents/test
cd "$var_dir" || exit 1	# Exit if we can't get into our target directory
+ cd /root/Documents/test

# Prompt for, get, and verify ZIP filename...
printf 'Enter name of file to be unzipped (with or without .zip extension): '
+ printf 'Enter name of file to be unzipped (with or without .zip extension): '
Enter name of file to be unzipped (with or without .zip extension): read var_name 
+ read var_name
example
var_base=${var_name%.zip}
+ var_base=example
if [ ! -r "$var_base".zip ]
then	printf '%s: %s not found or not readable.' "$IAm" "$var_base".zip >&2
	exit 2
fi
+ '[' '!' -r example.zip ']'
+ printf '%s: %s not found or not readable.'script example.zip
script: example.zip not found or not readable.+ exit 2

into that folder there is nothing...

Not sure I understand - you entered a file name that does not exist in the target directory? So the message should be OK, no?

how not exist? I am on desktop, and execute script from desktop....

OK. I misinterpreted your code... Try:

#!/bin/bash
IAm=${0##*/}	# Save last component of script name for diagnostics

var_dir="/root/Documents/test"
if [ -d "$var_dir" ]	# Exit if target directory does not exist.
then	printf '%s: Directory %s not found.' "$IAm" "$var_dir" >&2
	exit 1
fi

# Prompt for, get, and verify ZIP filename...
printf 'Enter name of file to be unzipped (with or without .zip extension): '
read var_name 
var_base=${var_name%.zip}
if [ ! -r "$var_base".zip ]
then	printf '%s: %s not found or not readable.' "$IAm" "$var_base".zip >&2
	exit 2
fi

# Unzip the named file into the target directory and move the file extracted
# from that archive with the extension ".c" in place of ".zip" for the archive
# to have the extension ".d" instead of ".c".
unzip -o "$var_base".zip -d "$var_dir" || exit 3
cd "$var_dir" || exit 4	# Exit if we can't get into our target directory
mv "$var_base".c "$var_base".d || exit 5

# Update the configfile.
ed -s configfile.txt <<-"EOF" || exit 6
	g/#SOMETHING="none"/s//SOMETHING="none"/
	w
	1,$p
	q
EOF

# Restart...
service something restart

so , i must put my zip file in /root/Documents/test?

---------- Post updated at 04:05 AM ---------- Previous update was at 03:55 AM ----------

and i if can notice you here do again archive with file .d extension, or just renamed?

No. I changed the code in post #12 to look for the .zip file in whatever directory you were sitting in when you invoked your script.

I'm sorry, but I can't parse that sentence. As in your original script, after unzipping archive.zip from whatever directory you were in when you started your script with all of the extracted files going into the directory /root/Documents/test it then attempts to rename /root/Documents/test/archive.c to be /root/Documents/test/archive.d where archive is the filename you entered at the prompt (with a trailing .zip removed if it was present).

No, no, there is a file in folder which contain name same as the name of the zip file. So when unzip, it change name of the that file, not zip archive.

We obviously have a language barrier here, but I believe the code I suggested in post #12 meets your stated requirements.

However, I am concerned that:

  1. all of your zip files are being unzipped into a single directory,
  2. a single configuration file is being edited every time you run this script, and
  3. the command to run after updating an existing source file or installing a new source file never changes.

All three of these points seem strange to me, but without knowing:

  1. what files are in your zip archives,
  2. why the file configfile.txt needs to be edited every time you install a new file with a .d filename extension, and
  3. what the command service something restart does

it is very hard to guess at what you are trying to do or to determine whether or not any of the code I suggested will be of any use to you.

  1. It is some sertificates
  2. Because it will allways go to another PC, not to the same
  3. Its just a restart of some service (apache, mysql, etc).

So for a brief explanation of task. You have one zip file that should be unziped to some folder. On of unziped file contains a extension that need to be renamed. Then config file must be changed, and restart some service. Thats all that is need to be done.

You don't seem to realize what your script is doing:

  1. OK. Having certificates in a file ending with the filename extension .c (which by convention is used to indicate that it is a C programming language source code file) is confusing, but now I understand why you might want to change the filename extension from .c to .d .
  2. No. Exactly the same change is made to the exact same file ( /root/Documents/test/configfile.txt ) every time your script is run. How does that identify a different server???
  3. Not some service. The same service every time you run your script. The command service something restart would seem to always restart the something service (not apache , not mysql ; nothing by the single service named something ).
  4. Not to some folder. Always to the same folder ( /root/Documents/test ) every time your script is run.

The only things that change every time your script is run are:

  1. the name of the zip archive and
  2. the name of the file that is renamed in the directory /root/Documents/test .

Everything else in your script is a constant.

but yeah, thats idea. Extensions are just a examples. I dont need to determine other machines. When I execute script it needs to do what to, unzip, copy, etc. So in every other machines it will be doing the same, thats the point.