Tag Info

Hot answers tagged

14

Most commands that accept --foo as an option also accept -- by itself as an "end of options, start of arguments" marker - so you could do: printf -- "--no-color\n--format-doc\n--no-profile\n" >> ~/.rspec-test But the more specific answer to your exact example is that the first argument to printf is a format specifier, and you're making things more ...


13

First, note that the -z test is explicitly for: the length of string is zero That is, a string containing only spaces should not be true under -z, because it has a non-zero length. What you want is to remove the spaces from the variable using the pattern replacement parameter expansion: [[ -z "${param// }" ] This expands the param variable and ...


5

Shell scripts are normally treated as if they were the same as any other kind of executable file, such as binaries, Python scripts, Perl scripts, or any other kind of script. They have a shebang at the top that directs the kernel to execute them through the shell. They are expected to be invoked the same way as any other command. As such, a new shell is ...


5

Perhaps the aws bash command is returning non-printable characters that you don't see with print(). Try removing them with tr: FOLDER=$(./aws get $BUCKET"/location.txt" | tr -cd "[:print:]")


5

GNU parallel is made for just this sort of thing. You can run your script many times at once, with different data from your input piped in for each one: cat input.txt | parallel --pipe your-script.sh By default it will spawn processes according to the number of processors on your system, but you can customise that with -j N. A particularly neat trick is ...


4

I'd suggest a different approach, avoiding the possible word-splitting issues of ls #!/bin/bash shopt -s nullglob for ext in jpg png gif; do files=( *."$ext" ) printf 'number of %s files: %d\n' "$ext" "${#files[@]}" # now we can loop over all the files having the current extension for f in "${files[@]}"; do # anything else you like with ...


4

If you're using GNU mv you have the following option. $ mv -b source/* dest/. This switch tells mv to push any files that collide in the dest/. directory to a backed up version, typically adding a tilde (~) to the end of the file, prior to moving files into the directory. Example Say I have the following sample directories with files. $ mkdir source ...


3

See the Wooledge wiki on tests and conditionals: -w FILE: True if the file is writable by you. So, you could test it with: [[ -w "$file" ]] If you aren't using bash, you could equally use [ -w "$file" ]


3

You don't need any elaborate pipelines for this. Moreover, you don't need to find anything - you already know where each file is, you just don't know which of them is newest. This is easily handled. To demo, here's my little test: mkdir ./dir1 ./dir2 ./dir3 for d in 1 2 3 do touch ./dir$d/samefile sleep 1 done That creates the test set. Now, which ...


2

My approach would be: List all files in the directory Extract their extension Sort the result Count the occurrences of each extension Sort of like this: ls | awk -F . '{print $NF}' | sort | uniq -c | awk '{print $2,$1}'


2

POSIXly: case $var in (*[![:blank:]]*) echo '$var contains non blank';; (*) echo '$var contains only blanks or is empty or unset' esac To differentiate between blank, non-blank, empty, unset: case ${var+x$var} in (x) echo empty;; ("") echo unset;; (x*[![:blank:]]*) echo non-blank;; (*) echo blank esac


2

The easy way to check that a string only contains characters in an authorized set is to test for the presence of unauthorized characters. Thus, instead of testing whether the string only contains spaces, test whether the string contains some character other than space. In bash, ksh or zsh: if [[ $param = *[!\ ]* ]]; then echo "\$param contains characters ...


2

First of all, let me ident your code to make it look in a more structured way(and to be easy to read). adduser openerp DIR="/var/run/openerp /var/log/openerp" for NAME in $DIR do if [ ! -d $NAME ]; then mkdir $NAME chown openerp.openerp $NAME fi done rm -rf openerp* After creating the user, the for loop will expand each ...


2

Put hosts, IP addresses in a file for example. hosts.txt contains the following 212.39.82.157 212.39.82.155 1.1.1.1 22.22.22.22 Create the script. #!/bin/bash ping_hosts(){ echo echo "*** Ping all hosts ***" echo "--------------------------------------------" count1=0 count2=0 start=$(date +"%d-%m-%Y-%T") hosts=( 1.1.1.1 2.3.3.4 4.5.6.6 ) while ...


2

Depending on what you want exactly to do, and the structure of your files, other possibilites may be available, such as : ls -1tq /dir/*/readme.txt | head -n 1 Returns the full name of the most recent readme.txt found in any subdirectory (not recursive) of /dir/ The usage of * as part of the path (not just as part of a filename) often being underknown, I ...


2

You need quotes around the result of the command substitution, i.e. "$(command)" I think e.g. (simulating your query with an echo): $ IFS=$'\t' read min max rows <<< "$(echo -e "Wed Jul 30 15:40:38 EDT 2014\tThu Jul 31 15:40:38 EDT 2014\t27")" $ echo "$min"; echo "$max" Wed Jul 30 15:40:38 EDT 2014 Thu Jul 31 15:40:38 EDT 2014 or use a (more ...


2

If you simply want to write a script that executes the commands one after the other, you have two real options: Write a bash script, and for the last command, invoke zsh to execute it (have one shell invoke another shell). Write a zsh script which in turn runs the four commands, forgetting about bash. While bash is a very common shell on Linux, there is ...


2

just type the command and it will be executed: #!/bin/bash ls -l if you want to run other shell scripts use: sh otherShell.sh or for executable files: . otherShell.sh gnuplot is as other commands, you can use it inside shell script: Example: #!/bin/sh lib=$1 old="output/old/$lib.dat" new="output/new/$lib.dat" gnuplot << EOF set logscale x ...


1

Put this at the top: if [ -z "$1" ]; then echo "Argument required." exit fi -z tests the argument string to see if it is of zero length. If it's not zero length, execution will continue.


1

$ pcregrep -Mo 'pin\(ABC\) (\{([^{}]++|(?1))*\})' file pin(ABC) { a b c d e f { abc } } If you don't have pcregrep but have GNU grep and it has been built with support for PCRE patterns and your file is not too big and doesn't contain NUL characters, you can do: grep -zPo 'pin\(ABC\) ...


1

For more portability, you can use perl: $ perl -MList::Util=reduce -le ' BEGIN { $dir = "." } opendir DIR,$dir; print shift @{(reduce {$a->[1] > $b->[1] ? $a : $b} map {[$_,(stat($_))[9]]} grep { -d and !/^..?$/ } readdir DIR)} ' .Skype Change $dir to whatever directory you want ...


1

No awk or sed necessary, since your filelist.txt is already in the perfect format. A few things in advance… I presume that there is no whitespace in the file names, and that a space separates the two filenames on each line. Also, I assume that filelist.txt terminates with a EOL before the EOF. Of course, make a backup of the directory before running this ...


1

Bash supports here documents and here strings to provide standard input to a program from a string. The following: ./myprogram <<EOT $lat1 $lon1 $filename1 EOT will give myprogram three lines of input, which are the values of the lat1, lon1, and filename1 variables. If you put that inside a loop that sets the variables appropriately each time around ...


1

No, 'Usage' is not a command. In the case of your example it is the output to be printed to the screen if there isn't an argument run behind the command. It's basically saying if you see $1 variable then continue, else print this information to the terminal to help explain. For example, lets say I run the commnad ./foobar. It will exit and print 'Usage: ...


1

You probably want: for i in n5 n25 do if [ ${#i} -eq 2 ]; then python two.py n5 elif [ ${#i} -eq 3 ]; then python three.py n25 fi done Note that: for goes with do ... done. if goes with then ... [elif; then] ... [else; then] ... fi. the integer comparisons need -eq (equal) instead of = (for strings) and are written within ...


1

When you write: alias thm="cd $SET_DIR/sites/all/themes/" you're expanding the value of SET_DIR at the time you define the alias. That means you get the same value every time you run the alias, even if you've changed the variable value in between. If you backslash-escape the $ then the variable will be dereferenced when you use the alias instead: $ ...


1

You have to indicate what to kill: kill -9 $(ps | grep "server1" | grep -v grep | awk '{ print $1 }') You can also use the trick: kill -9 $(ps | grep "server[1]" | awk '{ print $1 }')


1

As requested, below is a working bash script. I use "=" sign field delimiter within each user 'record', and a space is the record delimiter. Note, I added fictitious emails for completeness. #!/bin/bash # DATE=$(date '+%m-%d-%Y') bdays='08-02-2014=Prashant=abc@example.com 08-15-2014=prabhat=123@example.com 09-16-2014=Aks=xyz@example.com ...


1

I have extended your script, so that you can run it once on startup and it will do it's job between 9PM and 9AM. #!/bin/bash -· LOGFILE="/tmp/autotest_run_count.txt" trap "echo manual abort; exit 1" 1 2 3 15 RUNS=0 while [ 1 ] ; do· HOUR="$(date +'%H')" if [ $HOUR -ge 21 -a $HOUR -lt 9 ] ; then # run program libreoffice || exit 1 ...


1

Try this example awk 'BEGIN{ OFS="|"; print "Column1|Column2|Column3|Column4|Column5|Column6"}; NR > 1{print "IND", "INR", $6, $7, $8, $9;}' Inputdata.txt > Output.xls



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