Using a variable inside a file to cat another.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using a variable inside a file to cat another.
# 1  
Old 02-05-2008
Using a variable inside a file to cat another.

I have a question to do and it's somewhat confusing. It says, and I quote

"Create a file called file_1 with three lines of text in it. Create a shell variable called "f_name", assign it the string "file_1". Use the cat command and the variable "f_name" to display the contents of the file "file_1""

Now what I did was I made both files, and in "f_name" I wrote this.

#!/bin/sh
f_name="file_1"
cat $f_name

and this will work. However what I think is the professor wants me to use this shell scripts f_name variable in a cat command without having the script itself do the work.

Is there a way to do this?
# 2  
Old 02-06-2008
This is homework. We don't support homework on this forum.

And, no, your answer is not too far off. Just put those commands in a script.
# 3  
Old 02-06-2008
True Jim! This is homework but the poor guy didnt hide the fact, not like other questions Ive seen... And he shown he is trying, so why not help without solving the problem?
As Jim mentionned you are in fact very close...
Where were you asked to write a script?
If you do exactly (One point after another, I counted 3) what is being asked and I know by what you already wrote, you cant go wrong!

All the best
# 4  
Old 02-06-2008
Tools Are you missing the first part of the request?

#!/bin/sh
history | tail -3 >file_1
f_name="file_1"
cat $f_name


It appears that the first part of the instructions are to create a file with 3 lines of text. So, need something to generate 3 lines of text. (And there are many other ways to generate random text.)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - Trying to cat a file that is a variable

Thank you in advance for looking at this, I've scoured the internet and can't find the answer I'm looking for!! - I am new at bash script so please bare with me!! I have a script where I've identified individual files within a folder, the filename is then stored as a variable ($filename):... (3 Replies)
Discussion started by: paperbackwriter
3 Replies

2. Shell Programming and Scripting

Problem while displaying(cat) file content inside telnet loop .

Hi Team, Not getting the file output inside my email which i am sending from unix box. . Please refer the below code : #!/bin/sh { sleep 5 echo ehlo 10.56.185.13 sleep 3 echo mail from: oraairtel@CNDBMUREAPZP02.localdomain sleep 3 echo rcpt to: saurabhtripathi@anniksystems.com... (1 Reply)
Discussion started by: tripathi1990
1 Replies

3. Shell Programming and Scripting

How to store remote variable from remote cat file ??

I am trying to cat on a file located on remote server and assign it to remote variable. I have both local and remote variables. Running below script from local. test.sh J_NAME=XXX2 J_IP=XXX ssh $J_IP "ps auxw |grep java | grep -v grep |grep $J_NAME | awk '{print ... (2 Replies)
Discussion started by: oraclermanpt
2 Replies

4. Shell Programming and Scripting

To print value for a $variable inside a $variable or file

Hi guys, I have a file "abc.dat" in below format: FILE_PATH||||$F_PATH TABLE_LIST||||a|b|c SYST_NM||||${SRC_SYST} Now I am trying to read the above file and want to print the value for above dollar variables F_PATH and SRC_SYST. The problem is it's reading the dollar variables as... (5 Replies)
Discussion started by: abcabc1103
5 Replies

5. Shell Programming and Scripting

cat file with variable substitution

MyFile contains: ALTER TABLE $DBN.$TBN ADD $COL $TYP COMPRESS ($VAL); I need to cat the file and have it substitute all of the variables with their contents. cat MyFile does not work. The following works for the first line, but errors on the second line because of the paren: $ while read... (2 Replies)
Discussion started by: Phil27577
2 Replies

6. Shell Programming and Scripting

pls help with cat inside script

Hi, Am not very well versed with unix, i wrote a script which will make a new file using cat option like this, trans.sh #!/bin/sh cat > file1 echo how are you I wrote only this part since i want to use this concept for something else. What i want to ask is, this script when executed... (4 Replies)
Discussion started by: romsf
4 Replies

7. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

8. Shell Programming and Scripting

cat vs head vs readline get variable from txt file

I have a file with a single filename in it, which I want to assign to a BASH variable, so I've been trying: c=$(head -1 somefile) echo $c which outputs correctly, but them when I do ... somecommand $c it says it can't find the file, is that because it's grabbing the whole line, and... (5 Replies)
Discussion started by: unclecameron
5 Replies

9. Shell Programming and Scripting

Need help in sed command ( Replacing a pattern inside a file with a variable value )

Hello, The following sed command is giving error sed: -e expression #1, char 13: unknown option to `s' The sed command is echo "//-----" | sed "s/\/\/---*/$parChk/g" where parChk="//---ee-" How can i print the variable value from sed command ? And is it possible to replace a... (2 Replies)
Discussion started by: frozensmilz
2 Replies

10. Shell Programming and Scripting

variable= 'cat file|wc -l' String problems

Hi, does anybody knows about wc -l, how to transform it inot a just number? this script ALWAYS executes the command3!!, However, the value of BMU_RUNNING is 1 case $BMU_RUNNING in *0) command1 ;; *1) command 2;; *)command 3;; esac The... (3 Replies)
Discussion started by: Santiago
3 Replies
Login or Register to Ask a Question