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?
# 8  
Old 08-08-2016
Please show us the output from the command:
Code:
ls -l /root/Documents/test

Then show us the exact output you get when you use the following command to run your script:
Code:
bash -xv script_name

where script_name is an absolute pathname of the file containing your script.
# 9  
Old 08-08-2016
Code:
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...
# 10  
Old 08-08-2016
Quote:
Originally Posted by tomislav91
Code:
.
.
.
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?
# 11  
Old 08-08-2016
how not exist? I am on desktop, and execute script from desktop....
# 12  
Old 08-08-2016
OK. I misinterpreted your code... Try:
Code:
#!/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

# 13  
Old 08-08-2016
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?
# 14  
Old 08-08-2016
Quote:
Originally Posted by tomislav91
so , i must put my zip file in /root/Documents/test?
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.


Quote:
and i if can notice you here do again archive with file .d extension, or just renamed?
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).
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