BASH - read use a path as input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH - read use a path as input
# 1  
Old 04-30-2010
BASH - read use a path as input

I am trying to script simply data transfer. I would like to have the user input the source "SRC" (/Volumes/DriveName/Users/johnq123) and then name the directory that the copied information will go to, "DST" . put I can't get it to work -

Code:
#!/bin/bash

#=============================================
# data recovery attempt for HFS/HFS+ Drives
#=============================================

echo -n " "$USER", input path to source Volume User Folder, ex. /Volume/drivename/Users/"
read SRC
echo -n " "$USER", input last name and case number."
read DST

echo "Creating Destination Directory on Volume BACKUPS."

mkdir /Volumes/BACKUPS/$DST

echo "Ditto-ing Data to Destination Directory on Volume BACKUPS."

ditto $SRC/Desktop/*.* /Volumes/BACKUPS/$DST
ditto $SRC/Documents/*.* /Volumes/BACKUPS/$DST
ditto $SRC/Pictures/*.* /Volumes/BACKUPS/$DST

echo "Done."

# 2  
Old 04-30-2010
Quote:
Originally Posted by dropkick888
I am trying to script simply data transfer. I would like to have the user input the source "SRC" (/Volumes/DriveName/Users/johnq123) and then name the directory that the copied information will go to, "DST" . put I can't get it to work -
"*.*" is a DOS thing, and makes little to no sense in UNIX. Try just "*". It'd also help if you told us what happened, we can't tell from here.

You might also want to try just copying the directory instead of directory/*, since the way you're doing it might mash all the files together in one giant undifferentiated directory and runs the risk of having too many arguments.
# 3  
Old 04-30-2010
ok, so I changed the script a bit, and have included the output.

Code:
#!/bin/bash

#=============================================
# data recovery attempt for HFS/HFS+ Drives
#=============================================

echo " "$USER", input path to source Volume User Folder, ex. /Volume/drivename/Users/"
read SRC
echo " "$USER", input last name and case number."
read DST

echo "Creating Destination Directory on Volume BACKUPS."

mkdir /Volumes/BACKUPS/$DST

echo "Ditto-ing Data to Destination Directory on Volume BACKUPS."

ditto $SRC/Desktop/* /Volumes/BACKUPS/$DST
ditto $SRC/Documents/* /Volumes/BACKUPS/$DST
ditto $SRC/Pictures/* /Volumes/BACKUPS/$DST

echo "That's all she wrote..."

Code:
 testuser, input path to source Volume User Folder, ex. /Volumes/drivename/Users/
/Volumes/MacHDD/Users/test1
 testuser, input last name and case number.
testcase123
Creating Destination Directory on Volume BACKUPS.
Ditto-ing Data to Destination Directory on Volume BACKUPS.
That's all she wrote...
ditto: can't get real path for source
ditto: can't get real path for source
ditto: can't get real path for source

i think the issue is in
Code:
 read SRC

and the input, SRC, to that being a path. i need some way to use the inputted path as the source for ditto
# 4  
Old 04-30-2010
Try the suggestions I made earlier, as well as an ls. So instead of running ditto thrice, you can:

Code:
ls -ld "${SRC}"
ditto "$SRC" "/Volumes/BACKUPS/$DST"

# 5  
Old 04-30-2010
According to the ditto manpage the ditto command expects directory names as arguments.

So probably something like following should work:
Code:
ditto $SRC/Desktop $SRC/Documents $SRC/Pictures /Volumes/BACKUPS/$DST

Besides also according to the ditto man page you even don't need to manually create the $DST directory, since it will be created automatically if it does not exist.

I also recommend to code some basic error-checking, e.g. check if the $SRC file (directory) exists.
# 6  
Old 05-03-2010
Thank you for the help. so with all the suggestions the script works and reads as follows:

Code:
#!/bin/bash

#=============================================
# data recovery attempt for HFS/HFS+ Drives
#============================================= 

echo " "$USER", input path to source Volume User Folder, ex. /Volume/drivename/Users/"
read SRC

echo " "$USER", input last name and case number."
read DST

echo "Creating Destination Directory on Volume BACKUPS."

mkdir /Volumes/BACKUPS/$DST

echo "Ditto-ing Data to Destination Directory on Volume BACKUPS."

ls -ld "$SRC"

ditto "$SRC" "/Volumes/BACKUPS/$DST"

echo "That's all she wrote..."

any time I attempt to backup directories within $SRC ex.
Code:
 
ditto "$SRC/Desktop" "/Volumes/BACKUPS/$DST" 
ditto "$SRC/Documents" "/Volumes/BACKUPS/$DST" 
ditto "$SRC/Pictures" "/Volumes/BACKUPS/$DST"

the script errors out with the same
Code:
ditto: can't get real path for source
ditto: can't get real path for source
ditto: can't get real path for source

The reason for backing up these specific folders is that my institution is only allowed to backup Desktop, Documents, and Pictures folders (i.e. no music or movies)
so the question is now, how can I back up the Desktop, Documents, and Pictures, folder without specifing three SRC's ($SRC1, $SRC2, $SRC3)?
# 7  
Old 05-03-2010
I tried running the code in my machine, it just works fine. Below is the code that i tried
Code:
#!/bin/bash

#=============================================
# data recovery attempt for HFS/HFS+ Drives
#=============================================

echo " "$USER", input path to source Volume User Folder, ex. /Volume/drivename/Users/"
read SRC
echo " "$USER", input last name and case number."
read DST

echo "Creating Destination Directory on Volume BACKUPS."

mkdir -p /Volumes/BACKUPS/$DST

echo "Ditto-ing Data to Destination Directory on Volume BACKUPS."

ditto $SRC/Desktop/* /Volumes/BACKUPS/$DST
ditto $SRC/Documents/* /Volumes/BACKUPS/$DST
ditto $SRC/Pictures/* /Volumes/BACKUPS/$DST

echo "That's all she wrote..."

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash read input in case statement not working as expected

I'm having an issue with bash read input when using a case statement. The script halts and doesn't read the input on the first loop. if I hit enter then the scripts starts to respond as expected. Need some help here. defaultans=8hrs read -e -i $defaultans -p "${bldwht}How long would you like... (5 Replies)
Discussion started by: woodson2
5 Replies

2. Shell Programming and Scripting

Read input from Keyboard, do not proceed if no input

Hi, I am working on a script, which requests users to enter input. Ex: read -p "Please enter your email id:" email I don't want users skipping this entry, this has to be mandatory.I dont want to proceed without input. I can do a check if variable $email is empty and proceed if not.But, i... (7 Replies)
Discussion started by: aravindadla
7 Replies

3. Shell Programming and Scripting

Moving files from parent path to multiple child path using bash in efficient way

Hi All, Can you please provide some pointers to move files from Base path to multiple paths in efficient way.Folder Structure is already created. /Path/AdminUser/User1/1111/Reports/aaa.txt to /Path/User1/1111/Reports/aaa.txt /Path/AdminUser/User1/2222/Reports/bbb.txt to... (6 Replies)
Discussion started by: karthikgv417
6 Replies

4. Shell Programming and Scripting

[Bash] Read History function & Read Arrowkeys

Hi. How can I create a history function? (By "read" command or so) & How can I configure a read command so that the arrow keys are not displayed so funny? (^[[A) Thanks in advance. (4 Replies)
Discussion started by: sinnlosername
4 Replies

5. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

6. Shell Programming and Scripting

BASH - read does not wait for user input in some circumstances

Hello. I am running 2 scripts : script_1 and script_2 These scripts are run as root Script 2 contains : #!/bin/bash # # ~/bin/script_2 # E_BAD_PARAM=115 # date2stamp () { date --date "$1" +%Y-%m-%d___%H:%M:%S } # USER_NAME=$1 NB_PARAM=$# PARAM0=$0 (2 Replies)
Discussion started by: jcdole
2 Replies

7. Shell Programming and Scripting

Using read to prompt for editable user input in Bash 3

Below is a simple script to prompt for user input while suggesting an editable default value at the prompt: shortname=user1 read -e -i $shortname -p "Please enter the username you would like to add: " input USERNAME="${input:-$shortname}" Please enter the username you would like to add:... (3 Replies)
Discussion started by: woodson2
3 Replies

8. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

9. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

10. UNIX for Advanced & Expert Users

Help-prompt for path and take this as input in find command

HI , I am trying to wite a script that will prompt me saying " what is path that you want to find ?". once i specify the path, the script should put this path in the find command mentioned below and execute the script: find <path> -ctime +200 -type f -exec ls -l {} \; for example : ... (7 Replies)
Discussion started by: bsandeep_80
7 Replies
Login or Register to Ask a Question