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:"

  1. Relevant commands, code, scripts, algorithms:
#!/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
  1. The attempts at a solution (include all code and scripts):

  2. 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, ICS 240 Operating Systems • Section 0

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).

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:

file1
file2
file3

and the script:

#!/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:

chmod +x readlist

then see what happens when you run the command:

./readlist list_of_files

Does this help?

The first tar line is invormative. The rest should be self- explaining, no?

That will be an empty file - not empty, but just binary zeroes. How about man stat ?

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

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

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

can be understood as redirecting any 'error message' to /dev/zero.
Like:

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

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

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

hth

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:

/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 .

1 Like