Unix/Linux Go Back    


Homework & Coursework Questions Students must use and complete the template provided. If you don't, your post may be deleted! Special homework rules apply here.

Trouble with Shell Script Compressing file

Homework & Coursework Questions


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-28-2015
Luvs2drnk Luvs2drnk is offline
Registered User
 
Join Date: Nov 2015
Last Activity: 29 November 2015, 1:10 PM EST
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Trouble with Shell Script Compressing file

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:
You will create a shell script that performs the following action:
  • Use a parameter to pass a file that contains a list of file locations.
  • Read the list of file locations and store it as a variable
  • Using the list of file location create a compressed file, do not show any output from this command
  • Display the contents of the compressed file
  • Display the size of the newly created compressed file
Keep getting the following errors/outputs:
tar: Removing leading `/' from member names
tar: /phager/Desktop: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

The script still creates the compression file, but when I try to display the file size and file name with the stat command I get more info than I want, all I need to display is the file size and name after the echo command "The size of file1.tgz is:"


2. Relevant commands, code, scripts, algorithms:

Code:
#!/bin/bash
echo "Creating Compressed File from file1"
while read $file1; do
echo "Contents of file are:"
cat file1
tar cvzf file1.tgz /phager/Desktop
echo "The size of file1.tgz is:"
stat file1.tgz
done < file1


3. The attempts at a solution (include all code and scripts):



4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Leeward Community College, Pearl City, HI, USA, Peter Gross, ICS240, https://www.sis.hawaii.edu/uhdad/ava...201610&c=55462

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
Sponsored Links
    #2  
Old Unix and Linux 11-28-2015
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 22 September 2017, 2:27 AM EDT
Location: San Jose, CA, USA
Posts: 10,502
Thanks: 542
Thanked 3,675 Times in 3,135 Posts
You seem to be confused about how you reference parameters passed to a shell script, about how the variable name passed to and filled in by the shell's read built-n works, and about how to reference variables created in your shell script.

I would suggest re-reading the manual page for bash on your system (using the command: man bash) paying special attention to the discussion in the PARAMETERS section, in the EXPANSION section, and the description of the read utility in the SHELL BUILTIN COMMANDS section.

If you have a file named list_of_files containing:

Code:
file1
file2
file3

and the script:

Code:
#!/bin/bash
listfile="$1"
while read file
do	echo "Read filename $file from $listfile."
done < "$listfile"

in a file named readlist which you have made executable with the command:

Code:
chmod +x readlist

then see what happens when you run the command:

Code:
./readlist list_of_files

Does this help?
Sponsored Links
    #3  
Old Unix and Linux 11-29-2015
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 September 2017, 5:16 PM EDT
Location: Aachen, Germany
Posts: 11,365
Thanks: 297
Thanked 3,523 Times in 3,242 Posts
Quote:
Originally Posted by Luvs2drnk View Post
.
.
.
Keep getting the following errors/outputs:
tar: Removing leading `/' from member names
tar: /phager/Desktop: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
The first tar line is invormative. The rest should be self- explaining, no?

Quote:
The script still creates the compression file, but when I try to display the file size and file name with the stat command I get more info than I want, all I need to display is the file size and name after the echo command "The size of file1.tgz is:"
.
.
.
That will be an empty file - not empty, but just binary zeroes. How about man stat?

Last edited by RudiC; 11-29-2015 at 01:13 PM..
    #4  
Old Unix and Linux 12-16-2015
mr_zeratul mr_zeratul is offline
Registered User
 
Join Date: Dec 2015
Last Activity: 13 October 2016, 10:08 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
Keep getting the following errors/outputs:
tar: Removing leading `/' from member names

tar does it for your own protection. To uncompress, specify -C.


Code:
The script still creates the compression file, but when I try to display the file size and file name with the stat command I get more info than I want, all I need to display is the file size and name after the echo command "The size of file1.tgz is:"

du -h $file, will suffice I guess
Sponsored Links
    #5  
Old Unix and Linux 12-16-2015
sea sea is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 14 October 2016, 2:49 PM EDT
Location: Swissh
Posts: 1,285
Thanks: 256
Thanked 226 Times in 209 Posts
Quote:
Do now show any output of the command
can be understood as redirecting any 'error message' to /dev/zero.
Like:

Code:
tar cvzf "$file1.tgz" "$file1" 2>/dev/zero

Alternativly, you could redirect and appending (the double >>) to a logfile like:

Code:
tar cvzf "$file1.tgz" "$file1" 2>>"./$file_list.log"

hth
Sponsored Links
    #6  
Old Unix and Linux 12-16-2015
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 22 September 2017, 2:27 AM EDT
Location: San Jose, CA, USA
Posts: 10,502
Thanks: 542
Thanked 3,675 Times in 3,135 Posts
Quote:
Originally Posted by sea View Post
Quote:
Do now show any output of the command
can be understood as redirecting any 'error message' to /dev/zero.
Like:

Code:
tar cvzf "$file1.tgz" "$file1" 2>/dev/zero

... ... ...
Although /dev/zero can be used this way on most systems, the conventional device for dumping output into the trash can is /dev/null which is described by the standards as:

Code:
/dev/null  An empty data source and infinite data sink. Data written to /dev/null shall be
           discarded. Reads from /dev/null shall always return end-of-file (EOF).

The standards do not require systems to provide /dev/zero.
The Following User Says Thank You to Don Cragun For This Useful Post:
sea (12-16-2015)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compressing old files as zip file through script Ganesh L Shell Programming and Scripting 6 07-25-2013 09:23 AM
Linux/UNIX Bash Shell Script trouble help needed!! TomFord1 Homework & Coursework Questions 4 04-09-2013 01:35 PM
Changing the file name while compressing karumudi7 Shell Programming and Scripting 2 01-11-2013 09:49 PM
Trouble using substr function with Bourne shell script E2004 Shell Programming and Scripting 4 09-29-2005 10:57 AM
compressing two files in a script nimish UNIX for Dummies Questions & Answers 1 09-25-2005 11:13 PM



All times are GMT -4. The time now is 02:27 AM.