Tag Info

Hot answers tagged

13

Yes - the $0 variable gives the name of the script as it was run: $ cat example.sh #!/bin/bash script_name=$( basename $0 ) this_script=$( basename ${BASH_SOURCE} ) if [[ ${script_name} = ${this_script} ]] ; then echo "running me directly" else echo "sourced from ${script_name}" fi $ cat example2.sh #!/bin/bash . ./example.sh Which runs like: $ ...


5

Yes they ( [ -z "$VAR" ] and [ "$VAR" = "" ] ...) are equivalent. Both are testing for emptiness of $VAR (e.g. when it is not bound, or set to an empty string). As commented by Celada, bad things might happen when using [ "$VAR" = "" ] if VAR is some valid test (e.g. if VAR is -z), in particular when [ (or test) is not a shell builtin. But on bash or zsh ...


5

shift $((OPTIND-1)) (note OPTIND is upper case) is normally found immediately after a getopts while loop. $OPTIND is the number of options found by getopts. shift n removes n strings from the positional parameters list. Thus shift $((OPTIND-1)) removes all the options that have been parsed by getopts from the parameters list, and so after that point, $1 ...


4

Okay, let's break this down. A subshell executes its contents in a chain (i.e., it groups them). This actually makes intuitive sense as a subshell is created simply by surrounding the chain of commands with (). But, aside from the contents of the subshell being grouped together in execution, you can still use a subshell as if it were a single command. That ...


4

The GNU implementation of grep (also found in most modern BSDs though the latest versions are a complete (mostly compatible) rewrite) supports a -o option to output all the matched portions. LC_ALL=C grep -ao CDA | wc -l would then count all the occurrences. LC_ALL=C grep -abo CDA to locate them with their byte offset. LC_ALL=C makes sure grep doesn't ...


4

Combining @DarkHeart's answer with the environment variable BASH_SOURCE seems to do the trick: $ head example*.sh ==> example2.sh <== #!/bin/bash . ./example.sh ==> example.sh <== #!/bin/bash [ "$(basename $0)" = "$(basename $BASH_SOURCE)" ] && \ echo "running directly" || \ echo "sourced from $0" $ ./example2.sh sourced from ...


3

Have a look here Wikipedia/Shebang.


3

Fedora uses RPMs. Assuming the util-linux RPM is installed on your system, the command to show the pre-removal scriptlet is: rpm -q --queryformat '%{PREUN}\n' util-linux You can check if the "util-linx" RPM is installed with this command: rpm -q util-linux If you want to show all RPM scriptlets for util-linux, you can use this command: rpm -q ...


3

Get the file size: size="$(stat --printf="%s" "$path")" Get the path without the last extension: path_without_extension="${path%.*}" Compare the two: [ "${path_without_extension}.${size}" = "$path" ]


2

The answer is: You can not! Linux is no real time system. The idea of UNIX and therefore Linux, too, is to provide minimum answer times, while the system is shared between multiple users and system processes. Depending on when you start the command, you might have to wait for a important system process to give you your share of processor time. Further the ...


2

The standard way of doing this in Debian is with debconf (not to be confused with the Debian conference!). This allows packages to ask questions and act on the user's answers. The wiki linked above has details, including a link to a tutorial (using debconf is too involved to explain here).


2

The following script takes an argument like "file*.x" and applies it to find | sort to get a list of files to process. With thousands of files, you may get "too many arguments" by echo file*.x. #!/bin/bash prev= find . -maxdepth 1 -type f -name "$1" | sort -V | while read -r file; do file=${file#*/} # Remove leading ./ if test -n ...


2

Check for the existence of a PID from the same script. Supposing your script is called my_script.sh add this at the beginning of the script: #!/bin/bash for pid in $(pidof -x my_script.sh); do if [ $pid != $$ ]; then kill -9 $pid fi done


2

So you do not have tempfile (resulting in: command not found). Therefore your variable data is set to an empty string. When you try to redirect dialog ... 2> $data it tries to redirect stderr to a not existing value. And that is ambiguous. Your options are to change data=$(tempfile 2>/dev/null) to data="/tmp/mytmpfile" or to install tempfile by ...


2

Another more modular approach: not bash-specific get_ifconfig () { for iface do ifconfig "$iface" 2>/dev/null && return done } get_ifconfig eth1 eth2 eth3 | sed -n 's/.*inet addr:\([0-9.]\+\).*/\1/p'


1

As the API returns JSON objects, I'd use a JSON parser for it, combined with sed and grep. I've decided for jq, which is contained in the Ubuntu repo's, so it could be installed using sudo apt-get install jq; I don't know what distro you are using, so you'll have to find that by yourself if you want to use this solution. jq ".[]" <filename> | sed ...


1

I'm not familiar with R, but your script is attempting to run as a bash script, not an R script. Change your first line to #!/usr/bin/Rscript or possibly #!/usr/bin/env Rscript. You may need to alter the path to wherever R is on your system. According to http://stat.ethz.ch/R-manual/R-devel/library/utils/html/Rscript.html it should start like this example: ...


1

In zsh it's as easy as array=(*.sh(Nom)) The glob qualifier om causes the matches to be sorted by modification time (newest first), and N forces the array to be empty if there is no match (instead of causing an error). In other shells such as bash, there's no good way of sorting by time. You can use ls -t, but that can break because the output is ...


1

The first Line tells the computer which interpreter to use while executing the file Let's say you write a script using python, and while running this script you will use the python interpreter and how would computer know which interpreter to use, it will know through this line which is also called the Shebang, for python #!/usr/bin/env python print "Hello ...


1

Something like: awk -F, '{ # print first three columns printf("%s,%s,%s", $2,$1,$3); #for all other columns for ( i = 4; i < NF; i++ ) { # if column number every third if ( ( i - 4 ) % 3 == 0) { printf(",%s", $i); } } #print newline print ""; }' your_file.csv


1

Assuming you have all files to be validated from the beginning. #!/bin/bash seqno=1 for i; do if tail -1 $i | grep -q $(printf "%016d\$" $seqno); then echo " valid: $i" seqno=$((seqno+1)) else echo "invalid: $i" fi done Input: $ grep . EFEGECC* EFEGECC_20150101.txt:9999 TR SAP ...


1

Here is one solution - #!/bin/bash grep "^$1 " NYSE.txt NASDAQ.txt | sed 's/:/ /' | awk '{printf "Stock %s ( %s ) - ",$2,$1; for(i=3;i<NF;i++) printf "%s ",$i OFS;if(NF)printf"%s",$NF;printf ORS}' The first grep searches for the line that starts with symbol. The sed substitutes the ":" after file name from get output to a blank space. The awk ...


1

subshell (command) will execute command in subshell, this is usefull, if you have more then one command. (ls) | wc will pipe ls to wc, obviously you can write ls | wc (ls ; date) | wc will pipe the result of both ls and date to wc. using ls ; date | wc will result in only date being piped to wc. substitution $(command) will execute command and ...


1

I'm assuming by parent folder you just mean the folder with filename.txt. You can get find to print this folder name with -printf '%h\n' instead of the -exec. You can pipe this into a shell loop or xargs for example: find /path/ -name "filename.txt" -type f -mtime -2 -printf '%h\n' | xargs -i rsync ... {} /destination \; I think you need to add -R to ...


1

You really don't want to store this in a temporary file. There isn't any need, either, but it requires a bit of creative shuffling: data=$(dialog --passwordbox "Enter your password" 10 30 3>&1- 1>&2- 2>&3-) What that does is swap fd 1 and 2 around (the "X>&Y-" construct means, "move fd Y so it becomes fd X instead". Yes, that's ...


1

The following shell script, when given an argument of the form file1.x, generates a series of diffs. It increments the last series of digits in the file name (so you can start at file0.x or file42.x) and goes on until it finds a missing number. #!/bin/sh current=$1 suffix=${1##*[0-9]}; digits=${1%"$suffix"} digits=${digits##*[!0-9]}; ...


1

I did this a long time back in one of my shell scripts. Here is how I did it: ps aux | \ grep -P ${BASH_SOURCE[0]} | \ grep -v $$ | \ grep -P "bash" | \ grep -oP "^[[:alnum:]]+\s+\d+\s" | \ grep -oP "\d+" | \ xargs kill -9 The beauty of this method is that it will NOT kill the current running script itself, only the previous instances of it. A sample ...


1

So I took your hex string and printed it out to bytes, but I swapped the NULs for <spaces> (mostly because I can't figure on how to get a NUL in a grep pattern): time \ ( set x58 x5e x20 x20 xfe x5a x1e xda \ x48 x20 x20 x20 x0d x20 x03 x20 \ x07 x20 x20 x20 xcd x01 x20 x20 export ...


1

There's something odd going on with the command line - whether it is a file-system problem or something more elementary (like unprintable characters in a directory name). The error message "Unable to find a suitable output format for pipe:1" is due to the previous "-f flac" being ignored. I have tried renaming an mp3 to your stated problem filename, and ...


1

Another shift: : "${1?USAGE: "$0" files...}" while [ "$#" -gt 0 ] do [ -h "$1" ] printf "'%s' is %.$((!$?*4))s%s\n" \ "$1" "not " "a symbolic link." shift; done



Only top voted, non community-wiki answers of a minimum length are eligible