Bash script to read file location


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to read file location
# 1  
Old 09-17-2010
Bash script to read file location

I'm writing a bash script that reads a file location from a user, and I'm wondering how to get the script to accept tab to auto complete the directories that are input.
# 2  
Old 09-17-2010
Check if your version supports the -e option:

Code:
read -e ...

# 3  
Old 09-17-2010
Thanks..

Another related problem:

I'm querying the user for a file location/name to create a file to log information to... something like /home/username/info.txt

But if the user enters ~/info.txt

the program errors saying the file does not exist?

Is there a way to get the read operation to accept ~?
# 4  
Old 09-17-2010
Tilde expansion happens before parameter and variable expansion, so I suppose you should use something like this:

Code:
#!/bin/bash
 
read -ep'enter filename: '
 
ls -l "${REPLY/~/$HOME}"

# 5  
Old 09-17-2010
So, my code is:

Quote:
#!/usr/local/bin/bash
echo "Test read:"
read -e file
echo -n > $file
And the current output is:

Quote:
Test read:
~/testDir/text
./test: line 4: ~/testDir/text: No such file or directory
How would I resolve this? (testDir does exist)

Also assume that the user will not always enter ~/...

---------- Post updated at 11:10 AM ---------- Previous update was at 10:20 AM ----------

I've attempted different variations on this to no success..
# 6  
Old 09-17-2010
Try to change this:

Code:
echo -n > $file

to

Code:
echo -n > "${file/#\~/$HOME}"

# 7  
Old 09-17-2010
Interesting..

care to explain how that works?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

BASH script to read external file to perform text replacements?

Hi all, I have a moderate size (300 lines) BASH Shell script that performs various tasks on different source reports (CSV files). One of the tasks that it performs, is to use SED to replace 'non-conforming' titles with conformant ones. For example "How to format a RAW Report" needs to become... (3 Replies)
Discussion started by: richardsantink
3 Replies

2. Shell Programming and Scripting

Bash script to read a file from particular line till required line and process

Hi All, Am trying to write wrapper shell/bash script on a utility tool for which i need to pass 2 files as arugment to execute utility tool. Wraper script am trying is to do with above metion 2 files. utility tool accepts : a. userinfo file : which contains username b. item file : which... (2 Replies)
Discussion started by: Optimus81
2 Replies

3. Shell Programming and Scripting

Shell script to read specified value from file and echo to the same location to other file.

Hello. I want to to backup some "default:" values from a file do some other job and after restore that "default:" values back. The problem is that the source and destination file has a lot of default: strings in it but with different values... So.. Here is an example: A part of my source... (6 Replies)
Discussion started by: ausdim
6 Replies

4. Shell Programming and Scripting

'Couldn't read file' error in bash script with expect, sed and awk!

Ok, so I have a bash script with an embedded expect statement. Inside of the expect statement, i'm trying to pull all of the non-comment lines from the /etc/oratab file one at a time. Here's my command: cat /etc/oratab |sed /^s*#/d\ | awk 'NR==1'|awk -F: '{print \"$1\"}'|. oraenv Now,... (0 Replies)
Discussion started by: alexdglover
0 Replies

5. Shell Programming and Scripting

Bash Script Help...search then read from file: change text and pipe back...

Hello, I am trying to make a bash script that can pull data from a file and then change one part of said data. I want to search by username and pull the full line. That way there is a way to replace just one part of that line then return it back to the file. My Data is stored like: ... (1 Reply)
Discussion started by: serverfull
1 Replies

6. Shell Programming and Scripting

Write shelll script to read file location

hi all i have a problem how to read file location..I read file as FILE=/home/tmp/new.file.but t is not useful for me.But i want my script read file location where the file is and copy in directory at boot time. Every time of booting files are copied in respective folder.please help !!!!:) (2 Replies)
Discussion started by: shubhig15
2 Replies

7. Shell Programming and Scripting

Problem when concatinating wildcard onto file location in bash script

I am having difficulty with the following script: #! /bin/bash filelist=~/data/${1}* ~/./convertFile $filelist ~/temp/outputEssentially, there are a large number of files in the directory ~/data, each with a four-letter code at the beginning (eg. aaaa001 aaaa002 bbbb001 bbbb002 etc). The... (11 Replies)
Discussion started by: Lears_Fool
11 Replies

8. Shell Programming and Scripting

Bash Script to read a file and parse each record

Hi Guys, I am new to unix scripting and I am tasked to parse through a CSV file delimited by #. Sample: sample.csv H#A#B#C D#A#B#C T#A#B#C H = Header D = Detail Record T = Tail What I need is to read the file and parse through it to get the columns. I have no idea on how... (8 Replies)
Discussion started by: 3vilwyatt
8 Replies

9. Shell Programming and Scripting

Read Write byte range/chunk of data from specific location in file

I am new to Unix so will really appreciate if someone can guide me on this. What I want to do is: Step1: Read binary file - pick first 2 bytes, convert from hex to decimal. Read the next 3 bytes as well. 2 bytes will specify the number of bytes 'n' that I want to read and write... (1 Reply)
Discussion started by: Kbenipel
1 Replies

10. Shell Programming and Scripting

Bash copy file contents into an existing file at a specific location

Hi all I need to copy the entire contents of one file into an existing file at a specific location. I know the exact line number where I need to put it. It appears I would use either sed or awk to do this, but I have been unsuccessful so far: File A line 1 line 2 line 3 line 4 ... (6 Replies)
Discussion started by: gshepherd7
6 Replies
Login or Register to Ask a Question