Initialize file name bash shell - Linux


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Initialize file name bash shell - Linux

I am trying to initialize a file name in bash but not having much luck. For example, one of my bash scripts outputs a file named "FILE_1000G.vcf". I would like to rename FILE to match with the user's name. This is my code:


Code:
set -e
echo "Please enter your filename:"
read filename
rename 's/FILE/$filename/' *

The script prompts the user for their name. Say the user inputs John. The desired output woud be John_1000G.vcf


I keep getting the following error:


Global symbol "$filename" requires explicit package name (did you forget to declare "my $filename"?) at (user-supplied code).

Last edited by Geneanalyst; 12-01-2018 at 01:54 AM..
# 2  
What you have looks like it would try to change a file named FILE_1000G.vcf to instead have the literal name $filename_1000G.vcf assuming that the only file in the directory where you run this script is named FILE_1000G.vcf.

As you know, shell variable expansions are not expanded when $variable_name appears inside single quotes. And, if your system has a rename utility, it probably won't be happy with operands that do not contain the string you're trying to replace. (I know of several systems that have a rename() function in C; but I don't have access to any that have a rename utility.) So, in case your system does have a rename utility that does what I'm guessing you're trying to do, you might want to try:
Code:
set -e
echo 'Please enter your name:'
read username
rename "s/FILE/$username/" *FILE*

Assuming that you're using a POSIX conforming shell (which you have not specified), and using just standard utilities, you might want to try something more like:
Code:
echo "Please enter your name:"
read username
for filename in *FILE*
do	if [ -f "$filename" ]
	then	prefix=${filename%%FILE*}
		suffix=${filename#*FILE}
		mv "$filename" "$prefix$username$suffix"
	fi
done

This should work in any directory containing zero or more regular files with filenames that contain the literal string FILE one or more times (replacing only the first occurrence of FILE if FILE appears more than once in a filename).
This User Gave Thanks to Don Cragun For This Post:
# 3  
Just a small detail:

Quote:
Originally Posted by Don Cragun
Code:
echo 'Please enter your name:'
read username

Code:
read username?'Please enter your name:'

Will display the prompt and take the input just as well.

bakunin
# 4  
Quote:
Originally Posted by Don Cragun
What you have looks like it would try to change a file named FILE_1000G.vcf to instead have the literal name $filename_1000G.vcf assuming that the only file in the directory where you run this script is named FILE_1000G.vcf.

As you know, shell variable expansions are not expanded when $variable_name appears inside single quotes. And, if your system has a rename utility, it probably won't be happy with operands that do not contain the string you're trying to replace. (I know of several systems that have a rename() function in C; but I don't have access to any that have a rename utility.) So, in case your system does have a rename utility that does what I'm guessing you're trying to do, you might want to try:
Code:
set -e
echo 'Please enter your name:'
read username
rename "s/FILE/$username/" *FILE*

Assuming that you're using a POSIX conforming shell (which you have not specified), and using just standard utilities, you might want to try something more like:
Code:
echo "Please enter your name:"
read username
for filename in *FILE*
do    if [ -f "$filename" ]
    then    prefix=${filename%%FILE*}
        suffix=${filename#*FILE}
        mv "$filename" "$prefix$username$suffix"
    fi
done

This should work in any directory containing zero or more regular files with filenames that contain the literal string FILE one or more times (replacing only the first occurrence of FILE if FILE appears more than once in a filename).

Thanks Don, I liked your 1st code, just learned something!
# 5  
Quote:
Originally Posted by bakunin
Just a small detail:



Code:
read username?'Please enter your name:'

Will display the prompt and take the input just as well.

bakunin
Hi bakunin,
It will with a recent ksh (such as version 93u+). It might with a recent bash, but not with bash version 3.26 (the version included with the latest macOS release) where the following command:
Code:
read x?'Prompt: '

prints no prompt, blocks waiting to read a response, and after a response has been entered gives the diagnostic:
Code:
bash: read: `x?Prompt: ': not a valid identifier

Other shells might do what you want, might print a diagnostic without waiting for input, or might behave in a manner similar to that shown above.

I tend to use an echo or printf to print the prompt before the read, so I get the desired results with any version of any shell that is based on Bourne shell syntax (since the mid 1970's when using echo or since the late 1980's when using printf). But, if you know you only want to run your script on a system where the shell you're using has a built-in read utility with prompting, by all means take advantage of the shortcut.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 6  
Quote:
Originally Posted by Don Cragun
Hi bakunin,
It will with a recent ksh (such as version 93u+). It might with a recent bash, but not with bash version 3.26 (the version included with the latest macOS release)
hmm, interesting. I didn't know that. My main shell (ksh88, AIX) does it and i always thought that bash does it too - it seems i was just lucky the few times i used bash.

bakunin
This User Gave Thanks to bakunin For This Post:
# 7  
Quote:
Originally Posted by bakunin
hmm, interesting. I didn't know that. My main shell (ksh88, AIX) does it and i always thought that bash does it too - it seems i was just lucky the few times i used bash.

bakunin
You're right. According to the book "THE New Kornshell Command and Programming Language" published in 1995, read issuing prompts was supported in at least some versions of ksh88 as well as in all versions of ksh93.

This is one of many features that bash adopted from ksh. I don't know which version of bash first included it.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #96
Difficulty: Easy
Unix and Unix-like operating systems are a family of computer operating systems that are derived from the original Unix System developed at Xerox Park.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX Korn Shell to Linux Bash

Migrating Unix batch jobs (Korn Shell) running in HP-UX server to Linux environment. Hi All Please help me to understand the easiest way to migrate Kernel Shell scripts to Linux Bash. Also let me know 1. Any automated scripts or tools available for this. 2. Challenges and issues... (5 Replies)
Discussion started by: cpremesh
5 Replies

2. Fedora

Bash shell problem on Fedora Linux

My shell environment is bash and desktop environment is LXDE. When I use the up and down button on the keyboard to view the command history on bash shell, many times part of the command from the history remains on the line. For example /home/milhan > ssh somedomain.org /home/milhan > then when I... (5 Replies)
Discussion started by: milhan
5 Replies

3. Shell Programming and Scripting

Linux/bash Script only working if executed from shell prompt

Hi, maybe I'm asking a VERY dumb question, but would anybody out there tell me, why this f****** script won't work if executed as a cronjob, but works fine if executed from a shell prompt? #! /bin/bash set PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin date >>... (3 Replies)
Discussion started by: beislhur
3 Replies

4. Shell Programming and Scripting

Initialize aliases during shell script execution

Hello, I am trying to run a shell script that tests the connectivity to all the sftp servers and email if any one of them is down. There are aliases for all the servers with sftp command prefix in a '.alias' file. But when I use the alias name to run it as a command in my shell script to test... (3 Replies)
Discussion started by: Dippu
3 Replies

5. Homework & Coursework Questions

Linux/UNIX Bash Shell Script trouble help needed!!

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 2. Shell Bash Script 3. !/bin/bash if echo no directory then mkdir -p /home/AC_Drywall elif ; then echo "$dir already exist" fi (4 Replies)
Discussion started by: TomFord1
4 Replies

6. Red Hat

bash Linux login file

Hello all new to linux here... Please help,... when using Pico how can I open and edit the bash Linux login file (stored in your Linux root directory). I would like to add the new commands for items listed below. a) display contents of a directory => mycd b) display date and time =>... (4 Replies)
Discussion started by: dannyboy02
4 Replies

7. Homework & Coursework Questions

LINUX Bash Shell Script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a bash shell script that presents work information of employees of a department from a company data... (1 Reply)
Discussion started by: help123
1 Replies

8. Shell Programming and Scripting

PLEASE HELP! LINUX BASH SHELL SCRIPT

PLEASE HELP! NEED LINUX SCTIPT Need to write a bash shell script to show information of employees of a department from a company data set. The script should accept a project number (1/2/3/10/20/30) and output * the name of the project * the name of the manager of the controlling... (1 Reply)
Discussion started by: help123
1 Replies

9. Shell Programming and Scripting

rm:command not found in linux Bash shell script

Hi All, Linux lxs3er06 2.6.9-67.ELsmp #1 SMP Wed Nov 7 13:58:04 EST 2007 i686 i686 i386 GNU/Linux Issue: While executing shell scripts in bash shell, following error messages are thrown: rm:command not found On doing little investigation, I added '/bin' to $PATH and on doing echo... (9 Replies)
Discussion started by: a1_win
9 Replies

10. Shell Programming and Scripting

initialize file to zero

hi can anyone tell how to initialize a file to zero (3 Replies)
Discussion started by: Satyak
3 Replies

Featured Tech Videos