Is it possible to change paths inside a bash script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is it possible to change paths inside a bash script?
# 1  
Old 08-05-2016
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:

Code:
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?

Code:
./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.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 08-05-2016 at 03:24 PM.. Reason: Added CODE tags.
# 2  
Old 08-05-2016
Not sure I understand your problem correctly. Did you consider the bash builtins read or select?
# 3  
Old 08-05-2016
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.
# 4  
Old 08-06-2016
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:
Code:
./script file.zip

and change the contents of script to:
Code:
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.
# 5  
Old 08-07-2016
I did my bash like this
Code:
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

Code:
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.

Last edited by tomislav91; 08-07-2016 at 03:00 PM..
# 6  
Old 08-07-2016
Quote:
Originally Posted by tomislav91
I did my bash like this
Code:
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

Code:
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:
Code:
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:
Code:
#!/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.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 08-08-2016
i tried your code. When I enter zip name, i god message that file is not found or not readable..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Make change to variable value inside of awk script

Hello, I have text data that looks like this, Mrv16a3102061815532D 6 6 0 0 0 0 999 V2000 -0.4018 1.9634 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 -1.1163 1.5509 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 -1.1163 0.7259 ... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

2. Shell Programming and Scripting

Aliases NOT working inside bash shell script

i have defined a function ln_s() for customizing the ln command in script1.sh. more script1.sh echo "Starting Execution" ./script2.sh echo "End of Execution" ln_s(){ ] && return ln -s "$1" "$2" } My script1.sh executes another script2.sh which has the following entry more script2.sh... (12 Replies)
Discussion started by: mohtashims
12 Replies

3. Shell Programming and Scripting

Run bash command inside zsh script

Hi, I would like to run following code in bash inside a zsh script. (In this case is output unfortunately very different if you run it in zsh). I tried to put "bash" in front of the code but I obtained following error message "bash: do: No such file or directory " eve though I merged the whole... (7 Replies)
Discussion started by: kamcamonty
7 Replies

4. Shell Programming and Scripting

Expect not working inside my Bash script

I am trying to execute expect command inside by small bash script to login into servers using key authentication method. My script is as follows: #!/bin/bash HOST=$1 /usr/bin/expect -c " spawn ssh -i /root/.ssh/id_rsa root@$HOST expect -exact "Enter... (3 Replies)
Discussion started by: John Wilson
3 Replies

5. Shell Programming and Scripting

Why commands inside bash script lost effectiveness?

Hi, I have a bash script to run many system commands on CentOS machine, but I am puzzled by some commands had no effect on parent environment. For example, I want to refresh the desktop xdg menu when some processes added or deleted items from desktop xdg menu. If I run "killall gnome-panel"... (4 Replies)
Discussion started by: hce
4 Replies

6. Shell Programming and Scripting

How to change the color inside email using shell script?

hi, i want to send an email from unix using mailx command. mailx -s "subject" "email@abc.com" < email.txt Email.txt contains some file names that are transferred successfully and some that failed. so the files that got failed to tranfer, should be displayed in red color in the mail. is it... (1 Reply)
Discussion started by: Little
1 Replies

7. UNIX Desktop Questions & Answers

Change name of files to their paths -- find loop

Dear All, I have many sub-folders but each of them have a file with same name but different data. I want to either move or copy them into a new folder but they need to have the path of where they are coming as part of their name... I have managed to find the files but dont know how to change... (2 Replies)
Discussion started by: A-V
2 Replies

8. Shell Programming and Scripting

Tracking change inside the script

we have more then 10 jobs scheduled in cronjob.. but we can see some of the script has been changed without any notification.. can we write any script which captures any changes inside the scripts with time of change and user name like .. or any other option apart from this ?? Plz help .. (4 Replies)
Discussion started by: netdbaind
4 Replies

9. UNIX for Dummies Questions & Answers

using awk inside bash script?

Hello, I'm trying to write a bash script that will query the current system time (OS X 10.6.6) and then convert the output from HH:MM:SS into time in seconds. The output of the system time command (systemsetup -gettime) is returned as: Time: HH:MM:SS so I wanted to use awk -F: to grab... (5 Replies)
Discussion started by: xaiu
5 Replies

10. Shell Programming and Scripting

Rewriting file paths in XML file within bash script

Hi guys, I'm working on a large set of scripts to move files around several servers and manipulate them for our staff. Basically we're shooting things, the videos hit a server and then need organised due to the language they've been shot in. Our XML (designed for Apple's Final Cut Pro) is right... (6 Replies)
Discussion started by: omfgbunnies
6 Replies
Login or Register to Ask a Question