Files test How to recover a specific variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Files test How to recover a specific variable
# 8  
Old 09-20-2016
Hi all !Smilie

I reopens the post because I still have a few questions concerning my script that it below :

Code:
declare -a missing
for name in $fichier1 $fichier2 $fichier3 $fichier4 $fichier5 $fichier6
do
        [[ -e $name ]] || missing[${#missing[*]}]=$name
done

if (( ${#missing[*]} == 0 ))
then
        echo -e "\nDate :   $date_form2\nFile :   $name_files\nHeight : $height_files\n"
        echo -e "Date :   $date_form2\nFile :   $name_files1\nHeight : $height_files1\n"
        echo -e "Date :   $date_form2\nFile :   $name_files2\nHeight : $height_files2\n"
        echo -e "Date :   $date_form2\nFile :   $name_files3\nHeight : $height_files3\n"
        echo -e "Date :   $date_form2\nFile :   $name_files4\nHeight : $height_files4\n"
        echo -e "Date :   $date_form2\nFile :   "$name_files5"\nHeight : $height_files5"
else
        echo "Missing: " ${missing[*]}
fi
        echo -e "\nDisk Used : $disk_used\nDisk Available : $disk_available\nDisk Used In Percent : $disk_percent\n"

exit 0

here, It work like I want but in the "else" section I would like the same thing that in the "then" section but with the path of missing file instead of the "$name_fileX".

I would like this ouput for example (file5 missing):

Code:
Date :   2016-09-19
File :   name_files
Height : 100K

Date :   2016-09-19
File :   name_files1
Height : 160K

Date :   2016-09-19
File :   name_files2
Height : 4.0K

Date :   2016-09-19
File :   name_files3
Height : 236K

Date :   2016-09-19
File :   name_files4
Height : 4.0K

Date :   2016-09-19
File :   Missing: name_file5
Height : 4.0K

Disk Used :  39M
Disk Available : 185M
Disk Used In Percent : 18%

Here, While hoping to have been clearly....Smilie thanks by advance SmilieSmilie

Last edited by Arnaudh78; 09-20-2016 at 03:29 PM..
# 9  
Old 09-20-2016
Please try this & modify as per your need. Iterate through the array missing in else part .
Code:
for missing_filename in "${missing[@]}"; do
echo "$missing_filename"
done

# 10  
Old 09-20-2016
I can't see the correlation between e.g. fichier2 and name_files3. Should there be any, why don't you do the output right in the for loop?
# 11  
Old 09-20-2016
I tried to write an example clearer:

Code:
date_form1=$(date +'%y-%m-%d') # Variable date format : YYMMDD
date_form2=$(date +'%Y-%m-%d') # Variable date format : YYYYMMDD

height_file1=$(du -h /home/$date_form2/* | sed -n '1p' | cut -c 1-4)
name_file1=$(du -h /home/$date_form2/* | sed -n '1p' | cut -c 6-)

height_file2=$(du -h /home/$date_form2/* | sed -n '2p' | cut -c 1-4)
name_file2=$(du -h /home/$date_form2/* | sed -n '2p' | cut -c 5-)  

height_file3=$(du -h /home/$date_form2/* | sed -n '4p' | cut -c 1-4)
name_file3=$(du -h /home/$date_form2/* | sed -n '4p' | cut -c 5-) 

height_file4=$(du -h /home/$date_form1/* | cut -c 1-5) 
name_file4=$(du -h /home/$date_form1/* | cut -c 6-) 

height_file5=$(du -h /home/$date_form1/* | cut -c 1-5)
name_file5=$(du -h /home/$date_form1/* | cut -c 6-) 

height_file6=$(du -h /home/file6-$date_form1.tar.gz | cut -c 1-5) 
name_file6=$(du -h /home/file6-$date_form1.tar.gz | cut -c 6-) 

disk_used=$(df -h | sed -n '9p' | cut -c 23-27)
disk_available=$(df -h | sed -n '9p' | cut -c 29-33)
disk_percent=$(df -h | sed -n '9p' | cut -c 35-38)

fichier1="/home/$date_form2/file1" 
fichier2="/home/$date_form2/file2"
fichier3="/home/$date_form2/file3"
fichier4="/home/$date_form1/file4"
fichier5="/home/file5-$date_form1.tar.gz" 
fichier6="/home/$date_form1/file6" 

affiche () {
if [ $name == "$fichier1" ];
then
	: (I wish nothing, No display)
else
	echo -e "\nDate :   $date_form2\nFile :   $name_file1\nHeight : $height_file1\n" ;
fi

if [ $name == "$fichier2" ];
then
	: (I wish nothing, No display)
else
	echo -e "Date :   $date_form2\nFile :   $name_file2\nHeight : $height_file2\n" ;
fi

if [ $name == "$fichier3" ];
then
	: (I wish nothing, No display)
else
	echo -e "Date :   $date_form2\nFile :   $name_file3\nHeight : $height_file3\n" ;
fi

if [ $name == "$fichier4" ];
then
	: (I wish nothing, No display)
else
	echo -e "Date :   $date_form2\nFile :   $name_file4\nHeight : $height_file4\n" ;
fi

if [ $name == "$fichier5" ];
then
	: (I wish nothing, No display)
else
	echo -e "Date :   $date_form2\nFile :   $name_file5\nHeight : $height_file5\n" ;
fi

if [ $name == "$fichier6" ];
then
	: (I wish nothing, No display)
else
	echo -e "Date :   $date_form2\nFile :   "$name_file6"\nHeight : $height_file6" ;
fi

}

declare -a missing
for name in $fichier1 $fichier2 $fichier3 $fichier4 $fichier5 $fichier6
do
     [[ -e $name ]] || missing[${#missing[*]}]=$name
done

if (( ${#missing[*]} == 0 ))
then
	echo -e "\nDate :   $date_form2\nFile :   $name_file1\nHeight : $height_file1\n" ;
	echo -e "Date :   $date_form2\nFile :   $name_file2\nHeight : $height_file2\n" ;
	echo -e "Date :   $date_form2\nFile :   $name_file3\nHeight : $height_file3\n" ;
	echo -e "Date :   $date_form2\nFile :   $name_file4\nHeight : $height_file4\n" ;
	echo -e "Date :   $date_form2\nFile :   $name_file5\nHeight : $height_file5\n" ;
	echo -e "Date :   $date_form2\nFile :   "$name_file6"\nHeight : $height_file6" ;
else
          
	affiche ;
	echo -e "\n\nMissing: " ${missing[*]}

fi

	echo -e "\nDisk Used : $disk_used\nDisk Available : $disk_available\nDisk Used In Percent : $disk_percent\n" ;

exit 0 ;

Its output :
Code:
Date :	2016-09-19
File :  /home/16-09-19/file1
Height : 100K

Date :	2016-09-19
File :  /home/16-09-19/file2
Height : 160K

Date :	2016-09-19
File :  /home/16-09-19/file3
Height : 4.0K

Date :	2016-09-19
File :  /home/16-09-19/file4
Height : 236K

Date :   2016-09-19
File :
Height :

Missing:  /home/16-09-19/file6

Disk Used :  39M
Disk Available : 185M
Disk Used In Percent : 18%

I took off the file 6 for provoke the function "affiche", like you note, it misses display of the file 5 and on position of file 6 it's blank.

Me, I would like :
Code:
Date :   2016-09-19
File :  /home/16-09-19/file1
Height : 100K

Date :	2016-09-19
File :  /home/16-09-19/file2
Height : 160K

Date :	2016-09-19
File :  /home/16-09-19/file3
Height : 4.0K

Date :	2016-09-19
File :  /home/16-09-19/file4
Height : 236K

Date :	2016-09-19
File :  /home/16-09-19/file5
Height : 236K

Missing:  /home/16-09-19/file6

Disk Used :	39M
Disk Available : 185M
Disk Used In Percent : 18%

or
Code:
Date :	2016-09-19
File :  /home/16-09-19/file1
Height : 100K

Date :	2016-09-19
File :  /home/16-09-19/file2
Height : 160K

Date :	2016-09-19
File :  /home/16-09-19/file3
Height : 4.0K

Date :	2016-09-19
File :  /home/16-09-19/file4
Height : 236K

Date :	2016-09-19
File :  /home/16-09-19/file5
Height : 236K

Date :	2016-09-19
File :  Missing:  /home/16-09-19/file6
Height : 


Disk Used :  39M
Disk Available : 185M
Disk Used In Percent : 18%

I hope I have helped you Smilie

Last edited by Arnaudh78; 09-20-2016 at 09:09 PM..
# 12  
Old 09-21-2016
As for displaying the full path, you have two choices: If you know that the file names are always relative pathes, the easiest is to prepend $PWD/. If this is not the case, or if you want to have the path "cleaned up" by not containing .. elements, you need to convert it to an absolute path explicitly. Google for "how to convert a relative path to an absolute path".

Aside from this, the logic of your script is flawed. You obviously want to treat each file separately, i.e. create for each file some information, whether it is missing or not. You have (at least) two choices to achieve this:

The first ist to output the desired information INSIDE the if-block where you detect that the file is missing. The second one - if, for whatever reason, you want to keep problem DETECTION and problem REPORTING separate, I suggest a completely differnt approach:

Either use two arrays (one containing all the file names, and one containing a code for the problem occuring with each corresponding file, for instance "missing" or "ok"), or alternatively use only one associative array, where the key is the file name and the value is "missing" or "ok".

---------- Post updated at 08:58 AM ---------- Previous update was at 08:50 AM ----------

I just noticed - by re-reading your initial post - that you never indicated which shell you are using, so some of the suggestions (such as associative arrays) I gave might not work in your shell.

Hence, the first step would be that you decide on a certain shell language, before we can discuss, how exactly a problem can be solved.

Common language choices are:

- POSIX shell (most unconvenient to program, but maximum compatibility - this is often used by system administrators who have to manage a network of different Unix flavours or different shell versions)

- bash (chances are highest that, if you ask a colleague, s/he will know it)

- Zsh (IMO most convenient for shell programming)

- ksh (Many people like it; I can't comment on it, because of lack of experience)

I use for most of my shell programming Zsh, because it is really made for programmers, but bash is also a very popular choice.
# 13  
Old 09-21-2016
Thank for your reply, I use #!/bin/bash, So I work in Bash ? I'm beginner, I'm open at all.what do you recommend?

Last edited by rbatte1; 09-21-2016 at 09:03 AM.. Reason: Added ICODE tags
# 14  
Old 09-21-2016
Why don't you try something along this line:
Code:
for name in $fichier1 $fichier2 $fichier3 $fichier4 $fichier5 $fichier6
  do if [ -e $name ]
         then 	echo -e "Date :   $date_form2\nFile :   $name_file2\nHeight : $height_file2\n" ;
         else 	echo -e "\n\nMissing: " $name
     fi
  done

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to Recover Deleted Files

Hi, By mistake, executed the following command : rm -rf * and ALL files got deleted. But I need to get back these files as they are very very important. Please help me how to recover this file. Its Urgent for me please. Thanks in advance. (6 Replies)
Discussion started by: unx100
6 Replies

2. UNIX for Dummies Questions & Answers

Recover directory/files in unix

Hi Guys I accidently deleted a directory( and the files in it) in unix using 'rm -rf' :) Is there any procedure/script/command to recover the same? Your help is highly apperciated -thanks Subramanya (1 Reply)
Discussion started by: sgbhat
1 Replies

3. UNIX for Dummies Questions & Answers

how to recover deleted files in unix

Hi Experts, by mistake i deleted some files that are very important to the project. is there any way that i can recover those files,there is no backup for that but the details of the file we know. This will be a great help. Thanks (5 Replies)
Discussion started by: namishtiwari
5 Replies

4. UNIX for Dummies Questions & Answers

Recover data from 2 files then combine

Using dd or similar tools to recover data from 2 damaged cdroms, I need a way to then combine the 2 files, 1 from each cd, and make a good file: this all result from finding that certain cd's tops scratch easily even when using the "proper" cd markers, hence making the file useless, however the... (1 Reply)
Discussion started by: saint65
1 Replies

5. Shell Programming and Scripting

Is there a way to recover files deleted using rm command???

Hi All, I just mistakingly deleted some files using rm command.Is there a way to get it back?i work on Solaris 10 Thanks, Kumar (1 Reply)
Discussion started by: kumarsaravana_s
1 Replies

6. UNIX for Dummies Questions & Answers

How to test for a specific file size

Hello, In my shell program, I need to test for a specific size of a text file before it can be imported into an oracle table. If the size is less than that number, my program should stop processing. What is the correct command to do this test? Thanks! (1 Reply)
Discussion started by: GEBRAUN
1 Replies

7. AIX

recover deleted files

How to recover deleted files in AIX ? (1 Reply)
Discussion started by: vjm
1 Replies

8. UNIX for Dummies Questions & Answers

Recover deleted files

Is there a Unix tool, like in Novell, to recover accidentally deleted files? (2 Replies)
Discussion started by: kuultak
2 Replies
Login or Register to Ask a Question