Clear contents of specified directories, then return exit status


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Clear contents of specified directories, then return exit status
# 1  
Old 08-26-2016
Clear contents of specified directories, then return exit status

Hello, this is my first post here.

I'm attempting to write a bash shell script to rm the contents of a directory without deleting the directory, specifically in OS X 10.10 . Here's what I have:

Code:
function clear() {
USER="$USER"
DIR=$1

rm -rfv /Users/"$USER"/library/$DIR/*
}

clear caches
clear Saved Application State
clear LaunchAgents

if [ "$?" -eq "0" ]
then
echo "Command succeeded"
exit 0
else
echo "Command failed"
exit 1
fi

It's not actually clearing anything but the caches directory and also stating that my permission is denied on Xamarin Font services or something.

Code:
rm: /Users/joshuaevans/library/caches/com.xamarin.fontconfig/84c0f976e30e948e99073af70f4ae876-le32d4.cache-3: Permission denied
rm: /Users/joshuaevans/library/caches/com.xamarin.fontconfig/8d7231e6733a9725c81b40e9f55f11b1-le32d4.cache-3: Permission denied
rm: /Users/joshuaevans/library/caches/com.xamarin.fontconfig/b0a71e6bf6a8a1a908413a823d76e21f-le32d4.cache-3: Permission denied
rm: /Users/joshuaevans/library/caches/com.xamarin.fontconfig/CACHEDIR.TAG: Permission denied
rm: /Users/joshuaevans/library/caches/com.xamarin.fontconfig: Directory not empty

Any suggestions?
# 2  
Old 08-26-2016
What are the permissions on the folders telling you permission denied?
# 3  
Old 08-26-2016
First, make alterations to your code as per the red quotes
Code:
function clear() {
USER="$USER"
DIR="$1"

rm -rfv "/Users/$USER/library/$DIR"
}

clear caches
clear "Saved Application State"
clear LaunchAgents

The $DIR/* is superfluous in a find. BUT you are not shielding the spaces in your "Saved Application State" argument.

I suspect the errors in rm are causing the shell-script to end prematurely.

Second, have you examined the permissions of the files?
Code:
find /Users/$USER/library/caches/* -ls

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 4  
Old 08-26-2016
apmcd47, I agree with most of your proposals, but the question of YouNicks was: ... to rm the contents of a directory without deleting the directory. So $DIR/* is not superfluous. So this line should be:
Code:
rm -rfv "/Users/$USER/library/$DIR/*"

But this line is really superfluous:
Code:
USER="$USER"

And of course: problems with permissions cannot be solved with scripts.
# 5  
Old 08-27-2016
Quote:
Originally Posted by Ivo Breeden
apmcd47, I agree with most of your proposals, but the question of YouNicks was: ... to rm the contents of a directory without deleting the directory. So $DIR/* is not superfluous. So this line should be:
Code:
rm -rfv "/Users/$USER/library/$DIR/*"

But this line is really superfluous:
Code:
USER="$USER"

And of course: problems with permissions cannot be solved with scripts.
Hi Ivo,
I agree that the USER="$USER" is not needed. And, I agree that when $DIR expands to a string containing spaces, that expansion has to be quoted. But quoting the asterisk will not work unless you are trying to remove a file literally named *. I assume that you really meant:
Code:
rm -rfv "/Users/$USER/library/$DIR/"*

Hi YouNicks,
In addition to what has already been said, also be aware that checking $? only tells you the exit code of the last command executed before $? is expanded. You are only checking the exit status of the function call: clear LaunchAgents. There is no test to see whether or not the other two calls to clear succeeded.

Maybe you wanted something more like:
Code:
IAm=${0##*/}

function clear() {
	rm -rfv "/Users/$USER/library/$1/"*
}

clear caches &&
    clear 'Saved Application State' &&
    clear LaunchAgents

if [ $? -ne 0 ]
then
	printf '%s: Command failed.\n' "$IAm" >&2
	exit 1
fi
printf '%s: Command succeeded.\n' "$IAm"

# 6  
Old 08-27-2016
Quote:
Originally Posted by apmcd47
First, make alterations to your code as per the red quotes
Code:
function clear() {
USER="$USER"
DIR="$1"
[..]

[..]
Andrew
Hi Andrew, your quotation remarks are valid, but they are not necessary in the case of the variable assignment, since field splitting and wildcard expansions are not performed when assigning to a variable. Quote removal is performed, so
Code:
DIR="$1"

is exactly this same as:
Code:
DIR=$1

Quote:
4. Each variable assignment shall be expanded for tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal prior to assigning the value.
Shell Command Language:Simple Commands


--
The quotes are only necessary when you want to assign something that has a space in it, or certain special characters.
For example:
Code:
var="multiple fields divided by spaces (and for example special characters like parentheses)"


Last edited by Scrutinizer; 08-27-2016 at 03:39 AM..
# 7  
Old 08-27-2016
Just an observation...

OSX 10.10.x, and numerous other OSes, have clear as a transient command.

The function clear() overrides the transient command and if the transient command is needed eslewhere in the script in the future it will not work.

Therefore it is not a good idea to create functions with the same name as a transient cammand, (utility).

Better to use something like clr tp avoid errors and confusion...

Just a thought.

A simple example:-
Code:
#!/bin/bash

# clear is a transient command, (utility), to clear the screen.
clear

# This function is also called clear...
function clear()
{
	echo "This is the function."
}

# The function gets called NOT the transient cammand.
clear

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Want to get the exit status

Hi All, I am trying to create a zip file with all the txt files(these are in large number) in the current directory. I am able to do this operation sucessfully. After this i want to get the status of the tar command executed and do accordingly. When i am trying with the below code, the status... (3 Replies)
Discussion started by: paddu
3 Replies

3. Shell Programming and Scripting

How to clear contents of a file without deleting it.

Hi, I have a script which will use an input.txt file as an input file. I am providing data to this input file in the script and once the script is executed, I want to clear all the contents of this file as during the second time use of this script, I'll be appending the data in this input... (5 Replies)
Discussion started by: pat_pramod
5 Replies

4. Shell Programming and Scripting

Exit Status

I have a shell script (#!/bin/sh) that interacts with Appworx and Banner Admin. In my script I want to check the exit status of awrun before continuing. awrun can run for 10 seconds or it can run for over a minute. So my question is, will it go through my if statement before awrun may even be... (2 Replies)
Discussion started by: smkremer
2 Replies

5. Shell Programming and Scripting

command does not return exit status due to tee

Hi, I am using /bin/sh. I want to display the stdout and stderr on the terminal as well as save it in a file, so I'm using this command. gmake all 2>&1 | tee log But even if gmake fails, it's always giving 0 as exit status, i suppose because of tee. # false 2>&1 | tee Log # echo $? 0... (2 Replies)
Discussion started by: anand_bh
2 Replies

6. Shell Programming and Scripting

Clear Terminal After Exit

I have had a look around and can not find the answer, I dont think im searching for the right phrase. I have written a script to control common functions on my server, however when exiting the script the terminal starts directly below the script that was running... how can i clear this so it... (2 Replies)
Discussion started by: foz
2 Replies

7. Shell Programming and Scripting

How to get the exit status

Hi all, I'm running a program which return 1 upon success. But when encounters problem shell return 's '1' . How to differentiate between them the shell return value and script return value. Ex. function fn return '1' if executed successfully and '0' if failed. But when if shell encounters... (1 Reply)
Discussion started by: yhacks
1 Replies

8. HP-UX

Return of EXIT status ( $? )

I have the question: How return the exit code from then assign : VAR=$(command ) for ex. VAR=$(ls ....) VAREXIT=$? echo $VAREXIT VAREXIT is equal to 0 if the directory exist or not exist. WHI?? if i execute the command direct from line-command , the value of $? is different if... (1 Reply)
Discussion started by: ZINGARO
1 Replies

9. UNIX for Advanced & Expert Users

clear CLOSE_WAIT status

Hi, I have an application with a bug in it that keeps sockets in CLOSE_WAIT, which eventually freezes the server because the user account it runs under runs out of file handles. We have the bug fixed but can only release the fix with the next release. Does anyone know how I can clear the... (3 Replies)
Discussion started by: rein
3 Replies

10. Shell Programming and Scripting

exit status

i downloaded a text file from metalab.unc.edu called sh.txt and in this reference manual it refers to shell scripting exit status .. at the end of one of the examples that author gave an exit status of 127.. to what does a 127 exit status refer too and what is its purpose in the code. moxxx68 (1 Reply)
Discussion started by: moxxx68
1 Replies
Login or Register to Ask a Question