You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionextract(){if [ $#-lt 2 ];thenif [ -f$1 ] ;thencase$1in*.tar.bz2)
tar xvjf $1
;;
*.tar.gz)
tar xvzf $1
;;
*.bz2)
bunzip2 $1
;;
*.rar)
unrar x $1
;;
*.gz)
gunzip $1
;;
*.tar)
tar xvf $1
;;
*.tbz2)
tar xvf $1
;;
*.tgz)
tar xvf $1
;;
*.zip)
unzip $1
;;
*.Z)
uncompress $1
;;
*.7z)
7z x $1
;;
*)
echo"'$1' cannot be extracted via extract"return 1
;;
esacelseecho"'$1' is not a valid file"return 2
fifiif [ $#-ge 2 ];thenif [ -f$1 ] ;thencase$1in*.tar.bz2)
if [ -d$2 ];then
tar xvjf $1 -C $2else
mkdir -p $2
tar xvjf $1 -C $2fi
;;
*.tar.gz)
if [ -d$2 ];then
tar xvzf $1 -C $2else
mkdir -p $2
tar xvzf $1 -C $2fi
;;
*.bz2)
bunzip2 $1
;;
*.rar)
if [ -d$2 ];then
unrar x $1$2else
mkdir -p $2
unrar x $1$2fi
;;
*.gz)
gunzip $1
;;
*.tar)
if [ -d$2 ];then
tar xvf $1 -C $2else
mkdir -p $2
tar xvf $1 -C $2fi
;;
*.tbz2)
if [ -d$2 ];then
tar xvjf $1 -C $2else
mkdir -p $2
tar xvjf $1 -C $2fi
;;
*.tgz)
if [ -d$2 ];then
tar xvzf $1 -C $2else
mkdir -p $2
tar xvzf $1 -C $2fi
;;
*.zip)
if [ -d$2 ];then
unzip $1 -d $2else
mkdir -p $2
unzip $1 -d $2fi
;;
*.Z)
uncompress $1
;;
*.7z)
if [ -d$2 ];then
7z x $1 -o $2else
mkdir -p $2
7z x $1 -o $2fi
;;
*)
echo"'$1' cannot be extracted via extract"return 1
;;
esacelseecho"'$1' is not a valid file"return 2
fifi
}
export -f extract
exesudo function:
Allows run shell functions with sudo
#!/usr/bin/env bash# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ## EXESUDO# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### Purpose:# -------------------------------------------------------------------- ## Execute a function with sudo## Params:# -------------------------------------------------------------------- ## $1: string: name of the function to be executed with sudo## Usage:# -------------------------------------------------------------------- ## exesudo "funcname" followed by any param## -------------------------------------------------------------------- ## Created 01 September 2012 Last Modified 02 September 2012functionexesudo ()
{
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #### LOCAL VARIABLES:#### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #### I use underscores to remember it's been passedlocal _funcname_="$1"local params=( "$@" ) ## array containing all params passed herelocal tmpfile="/dev/shm/$RANDOM"## temporary filelocal filecontent ## content of the temporary filelocal regex ## regular expressionlocal func ## function source### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #### MAIN CODE:#### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #### WORKING ON PARAMS:# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~## Shift the first param (which is the name of the function)unset params[0] ## remove first element# params=( "${params[@]}" ) ## repack array## WORKING ON THE TEMPORARY FILE:# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
content="#!/bin/bash\n\n"## Write the params array
content="${content}params=(\n"
regex="\s+"forparamin"${params[@]}"doif [[ "$param"=~$regex ]]
then
content="${content}\t\"${param}\"\n"else
content="${content}\t${param}\n"fidone
content="$content)\n"echo -e "$content">"$tmpfile"## Append the function sourceecho"#$(type"$_funcname_")">>"$tmpfile"## Append the call to the functionecho -e "\n$_funcname_\"\${params[@]}\"\n">>"$tmpfile"## DONE: EXECUTE THE TEMPORARY FILE WITH SUDO# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sudo bash "$tmpfile"
rm "$tmpfile"
}
export -f exesudo
The text was updated successfully, but these errors were encountered:
extract function very useful:
exesudo function:
Allows run shell functions with sudo
The text was updated successfully, but these errors were encountered: