Tag Info

Hot answers tagged

6

The standard (POSIX sh and utilities) canonical legible ways would be: string comparison: if [ "$a" != 0 ] || [ "$b" != 100 ]; then... decimal integer comparison (0100 is 100, whether leading blanks are ignored or not depend on the implementation though). if [ "$a" -ne 0 ] || [ "$b" -ne 100 ]; then... integer comparison (0x64, 0144 are 100 (POSIX mode ...


5

From the fact that it says file $attachment rather than file "$attachment", I guess your script cannot handle filenames that contain spaces.  But, be advised that filenames can contain spaces, and well-written scripts can handle them.  Note, then: $ file "foo bar" foo bar: ASCII text $ file "foo bar" | cut -d' ' -f2 bar: One popular and highly ...


5

If you're running bash you can do it with regexp matching: if [[ $Y =~ (ERROR|ORA-) ]]; then echo error fi Or, if you insist, you can also do it with awk: if ! printf '%s\n' "$Y" | awk '/ERROR|ORA-/ {exit 1}'; then echo error fi The point is, awk uses regular expressions, not shell expression logic. The simplest approach is probably to just ...


4

The key bit of syntax you're missing here is: git commit -m "$(printf "Updated $submodule Submodule\n\n" ; git diff $submodule)" The use of the $() form of command substitution inside double quotes sends the output of git diff... to git commit as a commit message with newlines intact. I used printf here instead of echo to prepend the subject line since ...


3

Possible solution could be with using awk: awk -F";" '{ for (i = 2; i <= NF; i++) { printf("%s;%s\n", $1, $i); } }' file With awk -F";" we set FS(field separator) to ;. Then for every row(record) we start from field 2 to the last field (NF): for (i = 2; i <= NF; i++) and we print field 1 and current field ($i).


3

If you don't want to process hidden files: for f in my_dir/*.so; do case $f in (*/[ab].so) : ;; (*) chmod 777 -- "$f"; chown igor -- "$f" ;; esac done Note that setting file permission to 777 is very bad ideal, causing security hole and making chown command later wasted.


3

You can use find for all of that including the size (man find): -size n[cwbkMG] File uses n units of space. The following suffixes can be used: `b' for 512-byte blocks (this is the default if no suffix is used) `c' for bytes `w' for two-byte words `k' for Kilobytes (units of 1024 bytes) ...


2

Just edit: grep ttl <<< "$PONG" To: grep -q ttl <<< "$PONG" The output you get is from grep. With the -q flag grep is quiet.


2

This can be done using two ways: GUI way ==> Use Selenium Terminal way ==> Use ghost.py or phantomjs Terminal Way You can go ahead and use ghost.py or phantomjs. Read their documentations on how to use them here : Ghost.py and phantomjs An example using Ghost.py First install ghost.py. To install this, you will need pip. Hence, do this on a ubuntu ...


2

POSIX one: $ awk 'BEGIN{for(n=33;n<=90;n++)printf "%c",n}' !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ Perl one: $ perl -e 'print chr for 33..90'


2

I would circumvent the escaping and do: ... | cut -d' ' -f2 that way it is clear that you need a space between the delimiter character (specified by the three letters sequence ' ') and the following option. With -d\ -f2 it is easy to miss you should have done -d\ -f2.


2

If you use ANSI strings: header=$' ------------------------------------- Name\tAge\tGender\tAddress ------------------------------------- ' you have the correct translation of any ANSI escaped characters already in your variable. (Note: the newline characters could also be defined as \n, but it wouldn't add to legibility in your case, so I'd keep your ...


2

case $(file -b --mime-type - < "$attachment") in (text/*) printf '%s\n' "$attachment is probably text according to file" case $(file -b --mime-encoding - < "$attachment") in (us-ascii) echo "and probably in ASCII encoding" esac esac


2

So you have something like this: $ foo='my dir' $ ls $foo my: no file or directory found dir: no file or directory found Instead, try this: $ foo='my dir' $ ls "$foo" Or even better: $ ls "${foo}" Both of these methods shield the [:space:] character from being interpreted by the shell, and ensures that it is read as a [:space:] character, not the ...


2

You might use lftp instead of the regular ftp client. With lftp you can use mget -E /path/to/files which will delete the source files after succesful transfer. See http://lftp.yar.ru/lftp-man.html for the manual.


2

Using sudo: #!/bin/sh ip -s -s neigh flush all ufw enable sudo -Hu username sh -c '"/home/back/Downloads/tor-browser_en-US/Browser/start-tor-browser" --detach || ([ ! -x "/home/back/Downloads/tor-browser_en-US/Browser/start-tor-browser" ] && "$(dirname "$*")"/Browser/start-tor-browser --detach)' dummy %k -H: Sets the $HOME environment variable ...


1

Here is your script modified slightly: #!/bin/bash if files=$(find . -name "*.gz" -size -10k -mtime -1 -print) ; then echo "The backup test has failed!\n$files" | mail -s "BACKUP FAILURE" myemail@gmail.com fi You don't need to put this in a for loop; there is an implicit loop in the find comand. One debugging technique you can use if you haven't ...


1

Use arithmetic evaluation and parentheses to avoid ambiguity: if (( ($a != 0) || ($b != 100) )); then # some commands Or, if you want to deal with strings as well, use [[.


1

[ "$((a||b^100))" -eq 1 ] && some commands A shell's math expansion will handle the boolean && AND || OR and ! NOT conditions by evaluating the expression to either 1 for true or 0 for false. It will handle the bitwise & AND | OR and ^ XOR operators as well, but obviously those won't necessarily get you a 0 or 1, though a bitwise ...


1

There are two ways to go about this. 1) Run the script as a non-root user and use sudo to raise privileges to the root user (prefix the commands to be ran as root with sudo). or 2) Run the script as root user and use su to run the tor command as non-root user. su allows you to stipulate what user to run the command as and the -c option to specify what ...


1

Try this with ksh: while read A B C; do tmp=$(($A*$C/100)) echo $tmp done < foo.txt > out1.txt Output to out1.txt: 13721725 720357926 261414528 See: Performing arithmetic on variables in the Korn shell


1

Another option is to not use cut and to match a regex against the full output of file: #... isFile=$(file $attachment) if [[ "$var" =~ ^[^:]*:\ ASCII ]] #...


1

The problem occurs in cut -d\ -f2. Change it to cut -d\ -f2. To cut, the arguments look like this: # bash: args(){ for i; do printf '%q \\\n' "$i"; done; } # args cut -d\ -f2 cut \ -d\ -f2 \ And here is the problem. \ escaped the space to a space literal instead of a delimiter between arguments in your shell, and you didn't add an extra space so the ...


1

It looks like you're trying to redirect the output of home/dir/file.txt | awk '{print $2}' to the while loop; first I guess that the correct path should be /home/dir/file.txt (however this is just an assumption); second /home/dir/file.txt | awk '{print $2}' doesn't redirect the content of /home/dir/file.txt to awk, while < /home/dir/file.txt awk '{print ...


1

you should try awk '{print $2}' home/dir/file.txt | while read num do if [ "$num" = "0" ]; then echo "Number is equal to zero" else echo "number is not equal to 0" fi done for a mixed awk/bash solution. As other have pointed out, awk redirection occur later.


1

Perhaps like this: perl -e 'for(65..90) { printf "%c", $_ }' Or, if you insist to do it with printf(1) on Linux: printf $( printf '\\x%02x' $( seq 65 90 ) ) Or, with printf(1) on *BSD: printf $( printf '\\x%02x' $( jot - 65 90 1 ) )


1

Getting the creation date will be a tough one on a Linux system as it isn't stored. Related question: How to find creation date of file? Use the ls command to list files, try the options -a and -s to list all, and list their sizes. Use the command man ls on the command line to read a full description on the ls command


1

From man bash: set -u Treat unset variables and parameters other than the special parameters "@" and "*" as an error when performing parameter expansion. If expansion is attempted on an unset variable or parameter, the shell prints an error message, and, if not -interactive, exits with a nonzero status. POSIX states that, in the event of an expansion ...


1

(I'm using bash 4.2.53). For part 1, the bash man page just says "An error message will be written to the standard error, and a non-interactive shell will exit". It doesn't say an ERR trap will be called, though I agree it would be useful if it did. To be pragmatic, if what you really want is to cope more cleanly with undefined variables, a possible ...


1

I would split this task up in two elements, the first is that you need a script rerar that extracts and builds the rar and takes the name of a rar as parameter: #!/bin/bash R="$PWD"/"$1" # if realpath is available you can use R=$(realpath "$1") tmpdir=$(mktemp -d --suff rerar) pushd "$tmpdir" # extract preserving directory structure of the archive # ...



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