Unix - If file arguments already have - unix

###Takes filenames as arguments and makes those executable
#create file variable
file=$*
chmod 755 $file
if [ $? -eq 0 ] ; then
echo permission change suceeded
else
echo permission change failed
exit 0
fi
This is my current code - I'm wanting to add an extra echo which will be "already got executable permission" - how would i add a check onto this to check that if it has executable permission or not

if [ -x "$file" ]; then
echo "already got executable permission"
else
....
fi
Check
help test

perm="$(stat -c "%a" $file)"
if [$perm -eq 755]; then
...
else
...
fi

Related

How To Check For Directory On A Give Argument

If the argument is 0 then script should check directory called “App0” is in the windows path variable. If not exists, then add \App0 in the path. I Am Struggling To Understand ( If the argument is 0 ).
My Work So Far.
if [ -d "${Appo}" ]; then
echo "Appo Doesn't Exist."
mkdir Appo
echo "File Created"
fi
Thank You
#!/bin/sh
if [[ $# == 0 ]]
then
echo "zero args"
fi
for arg in "$#" # You might get more than one argument.
do
dir="App${arg}" # Make the name by combining the strings.
if [[ -d $dir ]]
then
echo "App$arg exists"
else
mkdir "$dir" # Be careful the name supplied may contain spaces.
echo "Created directory: $dir"
fi
done

How to tell script to look only into a specific folder

I'm trying to make a recycle bin for UNIX, so I have two scripts. 1 to delete the file and move it to the bin, the other script to restore the file back to its original location.
my restore script only works if the person gives the path to the deleted file.
ex: sh restore ~/trashbin/filename
How do I hardcode into my script so that I don't need to give the path to the deleted file it should already know to look in the trashbin for the file. My restore script works only when someone calls in the path to the file.
#!/bin/bash
rlink=$(readlink -e "$1")
rname=$(basename "$rlink")
function restoreFile() {
rlink=$(readlink -e "$1")
rname=$(basename "$rlink")
rorgpath=$(grep "$rname" ~/.restore.info | cut -d":" -f2)
rdirect=$(dirname "$rorgpath")
#echo $orgpath
if [ ! -d "$rdirect" ]
then
mkdir -p $rdirect
#echo $var
mv $rlink $rorgpath
else
mv $rlink $rorgpath
fi
}
if [ -z "$1" ]
then
echo "Error no filename provided."
exit 1
elif [ ! -f "$1" ]
then
echo "Error file does not exist."
exit 1
elif [ -f "$rorgpath" ]
then
echo "File already exists in original path."
read -p "Would you like to overwrite it? (y/n)" ovr
if [[ $ovr = y || $ovr = Y || $ovr = yes ]]
then
echo "Restoring File and overwriting."
restoreFile $1
grep -v "$rname" ~/.restore.info > ~/.restorebackup.info
mv ~/.restorebackup.info ~/.restore.info
fi
else
echo "Restoring file into original path."
restoreFile $1
grep -v "$rname" ~/.restore.info > ~/.restorebackup.info
mv ~/.restorebackup.info ~/.restore.info
fi
When you "remove" the file from the file-system to your trash-bin, move it so that its path is remembered. Example: removing file /home/user/file.txt should mean moving this file to ~/.trash/home/user/file.txt. That way, you'll be able to restore files to the original location, and you'll have auto-complete work, since you can do: sh restore ~/.trash/<TAB><TAB>

List files from directory without displaying them

I am writing a script where I need to list files without displaying them. The below script list the files while executing which I don't want to do. Just want to check if there are files in directory then execute "executing case 2".
ls -lrt /a/b/c/
if [ $? != 0 ]
then
echo "executing case 2"
else
echo "date +%D' '%TNo files found to process" >> $LOG
Testing the return code of ls won't do you a lot of good, because it'll return zero in both cases where it could list the directory.
You could do so with grep though.
e.g.:
ls | grep .
echo $?
This will be 'true' if grep matched anything (files were present). And false if not.
So in your example:
ls | grep .
if [ $? -eq 0 ]
then
echo "Directory has contents"
else
echo "directory is empty"
fi
Although be cautious with doing this sort of thing - it looks like you're in danger of a busy-wait test, which can make sysadmins unhappy.
If you don't need to see the output of ls, you could just make it a condition:
[ "$(ls -lrt a/b/c)" ] && echo "Not Empty" || echo "Empty"
Or better yet
[ "$(ls -A a/b/c)" ] && echo "Not Empty" || echo "Empty"
Since you don't care about long output (l) or display order (rt).
In a script, you could use this in an if statement:
#!/bin/sh
if [ "$(ls -A a/b/c)" ]; then
echo "Not empty"
else
echo "Empty"
fi

UNIX - Moving a deleted file back to it's original directory

All I want to do is move a file from my created recycle bin to it's own original path.
My code is as follows:
#!/bin/bash
restore ()
{
base=`basename $restore_file | cut -d"_" -f1`
echo "Your file $base has been restored"
mv deleted/$restore_file /$HOME/$base
}
restore_file=$1
if [ $# -eq 0 ]
then
echo "You have not entered a file"
elif [ -e deleted/$restore_file ]
then
restore
grep -v $(basename $restore_file) $HOME/.restore.info >> $HOME/.restore.inf o.tmp
cp $HOME/.restore.info.tmp $HOME/.restore.info
rm $HOME/.restore.info.tmp
else
echo "Your file "$restore_file" does not exist"
fi
I'm sure the error is in on line 7, just not sure how to rewrite. Any ideas?
Do you need to put a full path in before deleted?
You don't need the slash before $HOME, but that shouldn't be a problem.

unix shell script creating backup.sh

How to write a shell script named "backup.sh" which accepts one parameter, which would be a filename/directory.
Create a backup copy of that with the .bak appended to its name.Show message on success.
If the file/directory does not exist, show a proper message.
i did up to this point.please help me to figure this out
#!/bin/sh
#create_backup.sh
And add a .bak
bak="${backup.sh}.bak"
if [ "$#" -eq 0 ]
then
exit 1;
echo "File Succesfully backuped"
fi
cp ${1} "${1}.back"
echo "File is not found "
exit 0
#!/bin/bash -e
directory=$1
cp -r $directory $directory.bak
echo "Success"
obvious caveats with pathing/error codes/etc

Resources