LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-13-2008, 04:18 PM   #1
Steph90231
LQ Newbie
 
Registered: Sep 2008
Posts: 2

Rep: Reputation: 0
Hello, I am new to Linux and need some help with bash shell.


I am trying to wirte a program that takes three arguments, all representing file names. The script must verify that all names have been provided and if they are not, it should display an error message. If all 3 file names were given, the script should copy the contents of the first two files into the third. If the 3rd file contains data it should be overwritten. This is what I have so far and I can't seem to get it to work correctly.

#!/bin/bash

if [$# -eq 3]

then

f1="$1"
f2="$2"
f3="$3"

cat "f1" "f2" > "f3"

else

echo "please enter three file names"

fi







Thanks!

-Steph
 
Old 09-13-2008, 05:40 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
When posting problems, show the exact output or error messages, and include how you ran the command. Others here don't really want to guess what went wrong. I see one possible problem.

Also, include your code in code tags [ code ] and [ / code ] (without spaces) to maintain the format of your code.
 
Old 09-13-2008, 06:10 PM   #3
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
First, the test command (aka '[') needs to have a space on both sides of it:
Code:
if [ $# -eq 3 ]
Secondly, you store data in the variables the right way, but you need to reference the variable with a '$' in front of it when you need to access what is in it:
Code:
f1=$1
f2=$2
f3=$3

cat $f1 $f2 > $f3
Other than that, the script should work.
 
Old 09-13-2008, 06:14 PM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
Quote:
Originally Posted by CRC123 View Post
Other than that, the script should work.
It is likely to fail the requirement that "if the 3rd file contains data it should be overwritten".
 
Old 09-13-2008, 06:20 PM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,072

Rep: Reputation: 384Reputation: 384Reputation: 384Reputation: 384
Quote:
Originally Posted by Mr. C. View Post
It is likely to fail the requirement that "if the 3rd file contains data it should be overwritten".
I don't see why, other than if the file is read-only. Redirection will overwrite everything the file contains which is what the OP wanted.

I'd also quote the vars when catting them, otherwise you are going to get funky results when the file names have spaces in the middle.

You can also add a couple of checks to see if the files do actually exist and are readable (if [ ! -r $1 ]... etc. etc.)
 
Old 09-13-2008, 06:24 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
Quote:
Originally Posted by i92guboj
I don't see why, other than if the file is read-only. Redirection will overwrite everything the file contains which is what the OP wanted.
This is precisely why actual testing on the given environment is necessary, and why I asked the OP to produce the error experienced in the OPs environment:

Code:
$ touch c
$ echo hello > a
$ echo world > b
$ cat a b > c
-bash: c: cannot overwrite existing file
 
Old 09-17-2008, 10:37 PM   #7
Steph90231
LQ Newbie
 
Registered: Sep 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks guys,

I got it working, it was the spacing.

I have more projects coming up. I will probably have more questions.



-Steph
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux command shell bash for pg backroger Linux - Software 4 07-27-2008 09:03 PM
Custom Linux Shell instead of bash/sh CY83R-R0N1N Linux - General 5 02-15-2007 05:15 PM
Linux Bash shell Scrpt help samBl Linux - General 3 08-29-2006 05:29 PM
Linux Shell Scripting using BASH Help! fooforon Programming 5 02-05-2004 09:16 AM
linux question about bash, shell ?? albina Linux - General 3 05-15-2002 01:07 AM


All times are GMT -5. The time now is 08:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration