How can I check if file exists using patterns? - sh

I have a directory with full svn backups named like this:
name1.20100412.r9.bz2
name1.20100413.r10.bz2
name2.20100411.r101.bz2
name3.20100412.r102.bz2
...
I need to check if a backup file exists using name and revision number only. I tried test but it didn't work:
if [ -e name1.*.r9.bz2 ]; then echo exists; fi
[: too many arguments
How can I test if file exists?

You could do:
shopt -s nullglob
set -- name1.*.r9.bz2
if [ -n "$1" ]; then echo exists; fi
or
pattern="name1.*.r9.bz2"
if [ "$(echo $pattern)" != "$pattern" ]; then echo exists; fi
Edit: Added shopt -s nullglob.

Just check if the shell has expanded the pattern. This works in sh and bash:
pattern="name1.*.r9.bz2"
if [ "$(echo $pattern)" != "$pattern" ]; then
echo exists
fi
P.S. If the pattern itself can be a valid filename, you can add a -e check:
if [ -e "$pattern" -o "$(echo $pattern)" != "$pattern" ]; then
echo exists
fi

A slight variation on eugene y's answer:
test "$(echo name1.*.r9.bz2)" != "name1.*.r9.bz2" && echo exists

I would use:
if ls name1.*.r9.bz2 &> /dev/null ; then
echo exists
fi

Try something like
FILES=(`ls name1.*.r9.bz2`)
if [ ${#FILES[#]} -gt 0 ]; then
echo "Files!"
else
echo "No Files"
fi

Related post:
Check if a file exists with wildcard in shell script
It seems to have a good answer, using the return value of ls.

Related

How do I make a zsh function autocomplet from the middle of a word?

I use zsh and wrote a function to replace cd function. With some help I got it to work like I want it to (mostly). This is a followup to one of my other question.
The function almost works like I want it to, but I still have some problems with syntax highlighting and autocompletion.
For the examples, lets say your directories look like this:
/
a/
b/
c/
d/
some_dir/
I am also assuming the following code has been sourced:
cl () {
local first=$( echo $1 | cut -d/ -f1 )
if [ -d $first ]; then
pushd $1 >/dev/null # If the first argument is an existing normal directory, move there
else
pushd ${PWD%/$first/*}/$1 >/dev/null # Otherwise, move to a parent directory or a child of that parent directory
fi
}
_cl() {
_cd
pth=${words[2]}
opts=""
new=${pth##*/}
local expl
# Generate the visual formatting and store it in `$expl`
_description -V ancestor-directories expl 'ancestor directories'
[[ "$pth" != *"/"*"/"* ]] && middle="" || middle="${${pth%/*}#*/}/"
if [[ "$pth" != *"/"* ]]; then
# If this is the start of the path
# In this case we should also show the parent directories
local ancestor=$PWD:h
while (( $#ancestor > 1 )); do
# -f: Treat this as a file (incl. dirs), so you get proper highlighting.
# -Q: Don't quote (escape) any of the characters.
# -W: Specify the parent of the dir we're adding.
# ${ancestor:h}: The parent ("head") of $ancestor.
# ${ancestor:t}: The short name ("tail") of $ancestor.
compadd "$expl[#]" -fQ -W "${ancestor:h}/" - "${ancestor:t}"
# Move on to the next parent.
ancestor=$ancestor:h
done
else
# $first is the first part of the path the user typed in.
# it it is part of the current direoctory, we know the user is trying to go back to a directory
first=${pth%%/*}
# $middle is the rest of the provided path
if [ ! -d $first ]; then
# path starts with parent directory
dir=${PWD%/$first/*}/$first
first=$first/
# List all sub directories of the $dir/$middle directory
if [ -d "$dir/$middle" ]; then
for d in $(ls -a $dir/$middle); do
if [ -d $dir/$middle/$d ] && [[ "$d" != "." ]] && [[ "$d" != ".." ]]; then
compadd "$expl[#]" -fQ -W $dir/ - $first$middle$d
fi
done
fi
fi
fi
}
compdef _cl cl
The problem:
In my zshrc, I have a line:
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}' '+l:|=* r:|=*'
This should make autocompletions case insensitive and make sure I can start typing the last part of a directory name, and in will still finnish the full name
Example:
$ cd /a
$ cd di[tab] # replaces 'di' with 'some_dir/'
$ cl di[tab] # this does not do anything. I would like it to replace 'di' with 'some_dir'
How do get it to suggest 'some_dir' when I type 'di'?
The second matcher in your matcher-list never gets called, because _cl() returns "true" (exit status 0, actually) even when it has not added any matches. Returning "true" causes _main_complete() to assume that we're done completing and it will thus not try the next matcher in the list.
To fix this, add the following to the start of _cl():
local -i nmatches=$compstate[nmatches]
and this to the end of _cl():
(( compstate[nmatches] > nmatches ))
That way, _cl() will return "true" only when it has managed to actually add completions.

Listing SRC_URI of all packages/files needed to build Yocto image

I would like to list all files that bitbake will fetch when I bake an image.
Currently, I am able to get the SRC_URI of all files needed to bake a Yocto image by executing bitbake core-image-minimal -c fetchall and then parse log files.
Is there is a simpler way to get the same result without the need to download files ?
I am not sure bitbake supports such feature. Ideally, I am looking for a command that prints out the package name and lists all files with corresponding URL
> bitbake core-image-minimal -c fetchall --print-only
Generally bitbake doesn't provides such functionality.
But I was able to create a simple solution witch creating simple .bbclass file which is inherited in all recipes, by adding it into local.conf file, please see my steps in order to archive that:
Steps:
let's create a class print-src.bbclass file used to get and print SRC_URI variable (remember to store this class file in layer which is available in conf/bblayers.conf):
$ cat print-src.bbclass
python do_print_src () {
# should probably be indented
srcuri = d.getVar('SRC_URI', True).split()
bb.warn("SRC_URI look like: %s" % srcuri)
}
addtask do_print_src before do_fetch
Add INHERIT += "print-src" into Your conf/local.conf file
Edit: it is important to use bitbake --runonly option, that allows to run specific task of the taskgraph for the specified target (with --runonly option do_print_src needs to be used as print_src),
Edit: Please note that --runall=RUNALL and --runonly=RUNONLY was introduced with Yocto Sumo release 2.5,
$ bitbake core-image-minimal --runonly print_src
Loaded 1236 entries from dependency cache.
NOTE: Resolving any missing task queue dependencies
Build Configuration:
BB_VERSION = "1.37.0"
BUILD_SYS = "x86_64-linux"
NATIVELSBSTRING = "universal-4.8"
TARGET_SYS = "i586-poky-linux"
MACHINE = "qemux86"
DISTRO = "poky"
DISTRO_VERSION = "2.5"
TUNE_FEATURES = "m32 i586"
TARGET_FPU = ""
meta
meta-poky
meta-yocto-bsp = "master:13cc30cd7de4841990b600e83e1249c81a5171dd"
Initialising tasks: 100% |##########################################################################################################################################################################| Time: 0:00:00
NOTE: Executing RunQueue Tasks
WARNING: ptest-runner-2.2+gitAUTOINC+49956f65bb-r0 do_print_src: SRC_URI look like: ['git://git.yoctoproject.org/ptest-runner2']
WARNING: grep-3.1-r0 do_print_src: SRC_URI look like: ['http://ftp.gnu.org/gnu/grep/grep-3.1.tar.xz', 'file://0001-Unset-need_charset_alias-when-building-for-musl.patch']
...
...
NOTE: Tasks Summary: Attempted 201 tasks of which 0 didn't need to be rerun and all succeeded.
Summary: There were 202 WARNING messages shown.
Please see sample warning output log line:
WARNING: ptest-runner-2.2+gitAUTOINC+49956f65bb-r0 do_print_src: SRC_URI look like: ['git://git.yoctoproject.org/ptest-runner2'].
I patched poky to create *.src files in downloads that contains the effective fetch URL of the package.
bitbake/lib/bb/fetch2/__init__.py | 19 ++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)
diff --git a/bitbake/lib/bb/fetch2/__init__.py b/bitbake/lib/bb/fetch2/__init__.py
index b853da30bd..03e84e0919 100644
--- a/bitbake/lib/bb/fetch2/__init__.py
+++ b/bitbake/lib/bb/fetch2/__init__.py
## -1257,16 +1257,16 ## class FetchData(object):
# Note: .done and .lock files should always be in DL_DIR whereas localpath may not be.
if self.localpath and self.localpath.startswith(dldir):
- basepath = self.localpath
+ self.basepath = self.localpath
elif self.localpath:
- basepath = dldir + os.sep + os.path.basename(self.localpath)
+ self.basepath = dldir + os.sep + os.path.basename(self.localpath)
elif self.basepath or self.basename:
- basepath = dldir + os.sep + (self.basepath or self.basename)
+ self.basepath = dldir + os.sep + (self.basepath or self.basename)
else:
bb.fatal("Can't determine lock path for url %s" % url)
- self.donestamp = basepath + '.done'
- self.lockfile = basepath + '.lock'
+ self.donestamp = self.basepath + '.done'
+ self.lockfile = self.basepath + '.lock'
def setup_revisions(self, d):
self.revisions = {}
## -1607,6 +1607,15 ## class Fetch(object):
m = ud.method
localpath = ""
+ p = "%s.src"%ud.basepath
+ d = os.path.dirname(p)
+ if d != '':
+ bb.utils.mkdirhier(d)
+ with open(p, 'wb') as f:
+ data = "%s" % ud.url
+ f.write(bytes(data, 'ASCII'))
+ return True
+
if ud.lockfile:
lf = bb.utils.lockfile(ud.lockfile)
Running bitbake core-image-minimal -c fetchall results:
$> find downloads/ -name '*.src' | head -n 5
downloads/lzop-1.03.tar.gz.src
downloads/libtheora-1.1.1.tar.bz2.src
downloads/mpfr-3.1.5.tar.xz.src
downloads/makedevs.c.src
downloads/expat-2.2.0.tar.bz2.src
This is not an optimal solution but I hope that such feature gets its way to mainline stream.
I needed something like this and got part way there before now.
I can generate a messy list of URIs used by executing the following command:
bitbake -g zlib && cat recipe-depends.dot | \
grep -v -e '-native' | grep -v digraph | \
grep -v -e '-image' | awk '{print $1}' | \
sort | uniq | xargs -I {} -t bitbake -e {} | grep SRC_URI=
This gives you all the URIs and files used in a recipe and some comments.
Not a perfect solution but I will see if I can improve on it.
A bit manual and straight forward way is to build your image from scratch, and copy all the fetch logs, and go thru it.
$ mkdir fetch_logs
$ find tmp/work/ -type f -name log.do_fetch* | cpio -pdm fetch_logs
Now grep "Fetch(ing/er)" in this fetch_logs and it will provide info on which URL your recipe used to fetch the source. This will be helpful to see the final URI used to fetch in PREMIRROR based builds.
NOTE: if you have sstate-cache enabled and functional, you may not see do_fetch at all.
I wrote scripts based on the solution in the question.
Basically I need to make a cscope project of all the sources, and hence I need to list all URI and clone all repos.
I wrote 2 scripts 1) List all SRC_URI and Branches
2) Install(clone) all the code in a single directory
listURI.sh
#!/bin/bash
TMP_FILE="___x__2242230p.txt"
SRCH_FILE="log.do_fetch"
TIME=$(date +%Y-%m-%d-%H-%M)
OUT_FILE="Project-$TIME.txt"
DEF_BRANCH="master"
BRANCH=""
SRC_URI=""
function usage {
echo "Usage : $0 < -f | -g > <Component-List-file>"
echo "Options :"
echo " -f : Run bitbake fetch and collect URI"
echo " -g : Get URI from Already fetched Project"
}
if [ $# -eq 0 ]
then
usage
exit
fi
if [ "$1" == "-f" ] || [ "$1" == "-g" ];then
if [ "$1" == "-f" ] && [ -z "$2" ];then
echo "Should pass Project-Name after -f Option"
exit
fi
else
echo "Unknown Option"
usage
exit
fi
POKYROOTDIR=`basename "$PWD"`
#echo $POKYROOTDIR
if [[ $POKYROOTDIR != "build" ]];then
echo "You are not on poky/build (Sourced Poky) Directory"
exit 0
fi
POKYROOTDIR=$PWD
if [ "$1" == "-f" ];then
echo "Running === bitbake -c fetchall $2 -f --no-setscene =="
bitbake -c fetchall $2 -f --no-setscene
fi
if [ "$1" == "-g" ];then
if [ ! -d tmp/work ];then
echo " Please run == bitbake -c fetchall <image> -f --no-setscene == before this"
usage
exit
fi
fi
echo "Looking for URIs --- It may take couple of minuites!"
rm -rf $OUT_FILE
cd tmp/work
find . -name $SRCH_FILE -exec grep -i 'DEBUG: For url git' {} \; -print > $POKYROOTDIR/$TMP_FILE
cd $POKYROOTDIR
while IFS= read -r line
do
#echo "$line"
BRANCH=$(echo $line | awk -F"branch=" '/branch=/{print $2}' | sed -e 's/;/ /' | awk '{print $1}')
SRC_URI=$(echo $line | cut -d';' -f1-1 | awk -F"url" '/url/{print $2}' | awk '{print $1}' | sed -e 's/git:/ssh:/')
if [ ! -z "$BRANCH" ] && [ ! -z "$SRC_URI" ];then
echo "$BRANCH $SRC_URI" >> $OUT_FILE
elif [ ! -z "$SRC_URI" ];then
echo "$DEF_BRANCH $SRC_URI" >> $OUT_FILE
fi
if [ ! -z "$BRANCH" ];then
echo "Found URI : BRANCH:$BRANCH URI:$SRC_URI"
elif [ ! -z "$SRC_URI" ];then
echo "Found URI : BRANCH:Default URI:$SRC_URI"
fi
#echo "$BRANCH $SRC_URI" >> $OUT_FILE
done < "$TMP_FILE"
#echo "=================== OUT PUT ==========================="
#cat $OUT_FILE
rm -rf $TMP_FILE
echo "----------------INFO-----------------"
echo "Project Creation: Success"
echo "Project Path : $POKYROOTDIR"
echo "Project file : $OUT_FILE"
echo "-------------------------------------"
installURI.sh
#!/bin/bash
function usage {
echo "Usage : $0 <List-file>"
}
if [ $# -eq 0 ]
then
usage
exit
fi
if [ -z "$1" ];then
usage
exit
fi
if [ ! -z "$(ls -A $PWD)" ]; then
echo "Directory ($PWD) must be Empty.... else it will mess up project creation"
exit
fi
if [ ! -f $1 ];then
echo "list file ($1) not found"
usage
exit
fi
filename=$1
while read -r line; do
name="$line"
echo "Fetching Projects"
git clone -b $line
done < "$filename"
echo "----------------INFO-----------------"
echo "Project Creation: Success"
echo "Project Path : $PWD"
echo "-------------------------------------"

Create zsh autocompletion

I have a directory with projects and I create a function to cd to this directory.
projects_folder="/opt/work/projects/"
function pr () {
cd $projects_folder
if [ ! -z $1 ]; then
cd $1
fi
}
I want to create zsh autocompletion for this function
compdef '_path_files -W /opt/work/projects -/ && return 0 || return 1' pr
Also I create custom complete function
_pr(){
local -a list
for dir in $(ls $projects_folder); do
inside_dir=$projects_folder$dir
if [ -d $inside_dir ]; then
list=( $list $dir )
for d in $(ls $inside_dir); do
double_dir=$inside_dir/$d
if [ -d $double_dir ]; then
list=( $list $dir/$d )
fi
done
fi
done
compadd -a $list
}
compdef _pr pr
But both of these variants don`t work. Can somebody explain me that is wrong? My .zshrc here
UPD:
Solution is delete antigen apply from .zshrc, regarding this issue

A shortcut for some usually used commands with variables

I use these commands on a daily basis:
mkdir ______ && rar a -m0 -v200M ______/______.rar "XXXXXXXX" && rm -rf "XXXXXXXX" &&
par2 c -r10 -l ______/______.par2 ______/* && ~/newsmangler/mangler.py -c
~/.newsmangler.conf ______ && rm -rf ______
Where "XXXXXXXX" and "______" are two variables.
Is there a way to use a shortcut to make this easier since the only things that change are "XXXXXXXX" and "______"? I'm looking for something similar to:
mycommands _____ XXXXX
Couple things you may want to know:
- I use GNOME terminal.
- I don't have a root access.
Just write a function that accepts two arguments:
function just_do_it (){
( [ -z "$1" ] || [ -z "$2" ] ) && echo "No arguments given" && return 1
echo "do stuff with $1 and $2"
# notice to use double quotes, when processing the varibales
ls -l "$1"
}
The first line check if the two arguments are given. The argument can be accessed by $1 (first one) and $2 (second).
Edit: the function in your case would then for example look like this (a bit more readable formatted):
function just_do_it (){
( [ -z "$1" ] || [ -z "$2" ] ) && echo "No arguments given" && return 1
mkdir "$1" && \
rar a -m0 -v200M "$1/$1.rar" "$2" && \
rm -rf "$2" && \
par2 c -r10 -l "$1/$1.par2" "$1/*" && \
~/newsmangler/mangler.py -c ~/.newsmangler.conf "$1" && \
rm -rf "$1"
}

shell script: `[[` not working as expected

I have a script:
ENABLE_SYSLOG=true
test -r /etc/default/inotifywait && . /etc/default/inotifywait || exit 99
test -d $INOTIFY_FOLDER || exit 100
inotifywait -mrq -e ATTRIB --format '%w%f' "$INOTIFY_FOLDER" | while IFS= read -r FILE
do
if [ -f $FILE ];then
# If file
if [ `stat -c %a $FILE` != "664" ] ;then
CHMOD_LOG=$(chmod -v 664 "$FILE")
[[ "$ENABLE_SYSLOG" = true ]] && logger -t inotifywait -p user.info "$CHMOD_LOG" &
fi
else
# If directory
if [ `stat -c %a $FILE` != "2775" ] ;then
CHMOD_LOG=$(chmod -v 2775 "$FILE")
[[ "$ENABLE_SYSLOG" = true ]] && logger -t inotifywait -p user.info "$CHMOD_LOG" &
fi
fi
done
Why my condition
[[ "$ENABLE_SYSLOG" = true ]] && logger -t inotifywait -p user.info "$CHMOD_LOG"
Not work ?
If I remove
[[ "$ENABLE_SYSLOG" = true ]] &&
Then everything is fine.
I try to remove ampersand (&), try other condition (as you can see in example), try if instead of [[ - but all in vain.
Any condition is not work
[[ ... ]] is present in most advanced shells (ksh, bash, and zsh) but is missing in POSIX sh and in Ubuntu /bin/sh (which is a symlink to /bin/dash).
You can either replace [[ ... ]] with [ ... ] or test ... (both are equivalent, the second makes it clear that test is a command while [ ... ] appears as if it is special syntax), or (unless the script is explicitly invoked as sh FILENAME), start it with #!/bin/bash. Note that [[ ... ]] is parsed differently than test or [ ... ], as [[ is a reserved word, while [ and test are merely builtin commands. Specifically, field splitting and globbing are not done between [[ and ]], while = and == in this context do shell pattern matching if neither string is quoted. Furthermore, the =~ operator to do regexp matching is not present in test or [.