Results 1 to 6 of 6
A)) write a shell script that will
1-prompt the user for a file name
2-check to see if the file exists
3-create the file if the file does not exist
...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 08-19-2011 #1Just Joined!
- Join Date
- Aug 2011
- Posts
- 8
would anyone check this script if its right or wrong
A)) write a shell script that will
1-prompt the user for a file name
2-check to see if the file exists
3-create the file if the file does not exist
4-then go back and repeat step 1 if the file doesn't exist
#!/bin/bash
for i in filename
do
echo -n "enter a file name: "
read filename
if [ -f "filename" ]; then
echo "file is exist."
else
echo cat > “filename”
fi
done
========================================
B)- create a shell script that will
1) Prompt for four file names
2) Combine all of the data in the first three files into the fourth file
cat > file1
cat > file2
cat > file3
cat > file4
to combine …
cat file1 file2 file3 > file4
- 08-19-2011 #2Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,683
This is almost right, but you've got some syntax problems, and you don't need the loop, e.g.:
Note, it just creates an empty file, if it didn't exist - is that what you really want?Code:#!/bin/bash read -p "enter a file name: " filename if [ -f "$filename" ]; then echo "file $filename exists." else touch $filename fi
for this one, just loop the first code snippet 4 times and then do your cat command at the end, e.g.B)- create a shell script that will
1) Prompt for four file names
2) Combine all of the data in the first three files into the fourth file
cat > file1
cat > file2
cat > file3
cat > file4
to combine …
cat file1 file2 file3 > file4
Code:for i in `seq 1 4`; do (code above to get filename1 ... filename4) done cat $filename1 $filename2 $filename3 > $filename4
- 08-19-2011 #3Just Joined!
- Join Date
- Aug 2011
- Posts
- 8
thank you soo much for the response .... but for the second question the one with combine files is my code wrong
- 08-19-2011 #4Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,683
Your "cat > file1" code is not *wrong* - it depends on what you are doing. I guess you want to take STDIN (input typed by the user) and write that to a file named "file1", yes?
I thought you were trying to get the actual name of the file from the user.
- 08-19-2011 #5Just Joined!
- Join Date
- Aug 2011
- Posts
- 8
for this one, just loop the first code snippet 4 times and then do your cat command at the end, e.g.
[/QUOTE]Code:for i in `seq 1 4`; do (code above to get filename1 ... filename4) done cat $filename1 $filename2 $filename3 > $filename4
getting this error
./sf: line 2: code: command not found
./sf: line 2: code: command not found
./sf: line 2: code: command not found
./sf: line 2: code: command not found
./sf: line 4: $filename4: ambiguous redirect
- 08-19-2011 #6Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,683
That stuff in parenthesis was just pseudo-code; substitute the code from the first snippet. also, it would be better to make use of arrays, e.g.:
That will take four filenames from user input, and concatenate the first three given into the fourth. Note that it does no checking on the filenames given, so you'll get errors if a fileaname is given that does not exist, or nothing is entered (they just hit Enter).Code:declare -a filenames for i in `seq 1 4`; do read -p "Enter file name #$i: " filename filenames[$i]=$filename done cat ${filenames[1]} ${filenames[2]} ${filenames[3]} > ${filenames[4]}


Reply With Quote

