New answers tagged

1

The logical extension to your if test would be to use elif (else if) if [ -s /app/Symantec/virusdefs/definfo.dat ]; then cat /app/Symantec/virusdefs/definfo.dat elif [ -s /usr/symantec/virusdefs/definfo.dat]; then cat /usr/symantec/virusdefs/definfo.dat else cat /opt/Symantec/virusdefs/definfo.dat fi You might want to refactor as a loop for dir ...


1

It is possible to do that in bc: echo 'xor(10,15)' | bc -l logic.bc Or in hex: echo 'obase=16;ibase=16; xor(AA,FF)' | bc -l logic.bc Using the logic file from here. Just do wget http://phodd.net/gnu-bc/code/logic.bc to get it.


1

: ... elif [ -s 'the other file' ]; then : ... fi The syntax is documented in bash(1).


2

There's no need to use tee: you can assign your message to a variable and printf that using colors, and then append the message to the logfile: msg=$(printf "%-70s %s" "df -h response : " " ......... NORMAL (Completed in > printf "%s%s%s\n" "${red}" "$msg" "${normal}" printf "%s\n" "$msg" >>$log rather than printf "%-70s %s\n" "${red}df -h ...


0

Using bash, sed, and gforth, (but no tr, expr, echo or printf): mac="C4:B9:83:7F:FF:AC" gforth -e 'hex '${mac//:}' 1 - . cr' -e bye | sed 's/../&:/g;s/:.$//' Output: C4:B9:83:7F:FF:AB


23

Like this: echo $(( 0xA ^ 0xF )) Or if you want the answer in hex: printf '0x%X\n' $(( 0xA ^ 0xF )) On a side note, calc(1) does support xor as a function: $ calc base(16) 0xa xor(0x22, 0x33) 0x11


10

With any POSIX shell: $ printf '%#x\n' "$((0x11 ^ 0x22))" 0x33


6

gdb has powerful expression calculator: gdb -q -ex 'print/x 0xA ^ 0xF' -ex q A shell function: calc_gdb() { gdb -q -ex "print/x $*" -ex q;} calc_gdb 0xA ^ 0xF $1 = 0x5


0

The reason why an event-script fails to send a "growler" message is that mcabber closes the standard input, output and error streams when it runs an event command. You can see this in hooks.c: if ((pid=fork()) == -1) { scr_LogPrint(LPRINT_LOGNORM, "Fork error, cannot launch external command."); g_free(datafname); return; } if (pid =...


2

telnet was the traditional way to get a remote shell on a networked unix machine. rsh was the other. ssh is a relative newcomer in comparison. telnet typically talks to a remote telnet daemon (or "server" process). At that point you authenticate with a username and password to login and then you get a shell. So, yes, it's a method of getting a remote ...


3

awk is good for this, but you could do it simpler, with calcc() { awk "{\$$1=$2;print}" | column -t } Whether that's better or not is your choice.


0

This solution user "123" created for me on another question was able to strip suffixes reliably without mangling words. I wanted to come back and answer this question so that anyone seeking a similar solution could get a good answer. awk 'FNR==NR{a[$0 "s"]++;next}!($0 in a)' file.txt file.txt awk 'FNR==NR{a[$0 "ed"]++;next}!($0 in a)' file.txt file.txt awk '...


0

It should be just [$]> pushd . no?


0

pushd -n $(pwd) adds the current directory $(pwd) to the stack without changing directory. From help pushd in bash: Options: -n Suppresses the normal change of directory when adding directories to the stack, so only the stack is manipulated.


5

There must not be any spaces between a variable name and the equation mark. When there are spaces, the variable name is interpreted as a command, in this case the command host is run with parameters = and the host name.


0

bash has C-style for loops, but stylistically they're a little odd to use when you don't have to. And if you really do absolutely have to, you might be using the wrong language. (For this use case shell is fine, of course.) A much more shell-like way to do this (in my opinion) is: for i in {0..5}; do for j in $(seq "$i" 5); do echo "$i$j"; done; done ...


1

Maybe you are looking for a "chroot jail for ssh", if the users require a terminal. Otherwise, if you just need they can access to their homes, configure sshd to jail stfp users in their homes: add to sshd_config: Match group myGroup //Also can match users ChrootDirectory %h ForceCommand internal-sftp -u 0007 AllowTcpForwarding no ...


3

Your command $ find . -name 'segment*' | xargs -n1 -P4 sh someFunction.sh has the effect that at most four copies of the someFunction.sh shell script will be started (-P 4) in parallel (new ones will be spawed as soon as the old ones are done), each one getting one filename as its argument (-n 1). This means that each invocation of your script will look ...


2

You could use something like this assuming someFunction.sh is in your working directory. find . -name 'segment*' -print0| xargs -0 -n1 -P4 ./someFunction.sh The -print0 and -0 allow for files with spaces in the name (A common problem). In my someFunction.sh I have #!/bin/bash echo "Arg: " $1 cat $1 Which simply echo's out the file name then ...


1

for ((i = 0; i <= 5; i++)); do for ((j = i; j <= 5; j++)); do echo -e "${i}${j}\n" >> numbers.txt done done Should do the trick. I removed the space, I prefer using {}'s around my variables and \n puts in new line.


1

Building on Rahul’s answer: $ ls -ltr | grep -v '^d' | awk 'NR==2 {print $NF; exit}' | xargs -I '{}' mv -- '{}' ../complete/ Notes: You say you want to do something with “the oldest file”.  All the answers are assuming that you mean least recently modified.  If you mean least recently changed, say so; the answers will be slightly different.  If you mean ...


1

To copy the oldest file to ../complete: cp -v "$(find ../in -maxdepth 1 -type f -printf '%T@ %p\0' | sort -zn | \ sed -zn '1s/[0-9,\.]\+ //p')" ../complete To copy the all except the oldest to ../error: find ../in -maxdepth 1 -type f -printf '%T@ %p\0' | sort -zn | \ sed -zn '2,$s/[0-9,\.]\+ //p' | xargs -0 cp -vt ../error Explanation: find -...


1

To find oldest file and move that to your desired directory, use: # cd to ../in/ $ ls -lt | grep -v '^d' | tail -1 | awk '{print $NF}' | xargs -I '{}' mv '{}' ../complete/ Now that your oldest file has been moved out, you can move all files to ../error/ $ mv -- * ../error/ The -- after mv is necessary if you have any file/directory names starting with ...


1

$ pwd; pushd /tmp; pwd; popd; pwd /home/users/foo /tmp ~ /tmp ~ /home/users/foo Bash will keep a history of the directories you visit, you just have to ask. Bash stores the history in a stack and uses the commands pushd and popd to manage the stack. If you don't need multiple levels of directory history, you can also do: cd foo # do your stuff in ...


2

Tradition. The Single Unix Specification says (my emphasis): PS1 This variable is used for interactive prompts. Historically, the "superuser" has had a prompt of '#'. Since privileges are not required to be monolithic, it is difficult to define which privileges should cause the alternate prompt. However, a sufficiently powerful user should ...


3

Historically the original /bin/sh Bourne shell would use $ as the normal prompt and # for the root user prompt (and csh would use %). This made it pretty easy to tell if you were running as superuser or not. # is also the comment character, so anyone blindly re-entering data wouldn't run any real commands. More modern shells (eg ksh, bash) continue this ...


2

You can use a parallel shell such as clustershell or pdsh. This way, assuming you already set up a passwordless SSH authentication from a central machine, you can run a command on each of the 100 servers at the same time. You can also go further and do various groups in order to organize them logically. Lets assume your machines are named aws0, aws1, aws2, ...


3

The wc (word-count) utility is able to count lines in a file: $ wc -l num.txt ... or rather, it counts the number of newlines in the file, which most of the time is the same thing (actually, on a Unix system, that is defined as the same thing). The manual (on Mac OS X) states: "Characters beyond the final <newline> character will not be ...


1

After considering other options presented here, and not fully understanding how some of them worked I developed my own path_remove function, which I added to my .bashrc: function path_remove { # Delete path by parts so we can never accidentally remove sub paths PATH=${PATH//":$1:"/":"} # delete any instances in the middle PATH=${PATH/#"$1:"/} # delete ...


0

according to the Bash-scripting guide. Process ID (PID) of the script itself. The $$ variable often finds use in scripts to construct "unique" temp file names. if you run echo $$ in a script you see the output is different from PID of current shell.


1

From Advanced Bash-Scripting Guide: $$ is the process ID (PID) of the script itself. $BASHPID is the process ID of the current instance of Bash. This is not the same as the $$ variable, but it often gives the same result.


5

$$ is the process ID of the current shell instance. So in your case the number, 23019, is the PID of that instance of bash. The following should give you a better idea: ps -p $$


2

As user @muru says, it's not possible to do because you have already left the shell session behind when you get to the #!-line. However, depending on what your shell files do, there might be another solution. I'm guessing that they set environment variables that you use for some project. Let's call a project subtool (because that's a project I have). Then ...


7

No. By the time a shebang comes into play, you have already lost. A shebang is applied when a process is exec()'d and typically that happens after forking, so you're already in a separate process. It's not the shell that reads the shebang, it's the kernel.


4

You can use eval: $ set -a $ eval "$(command_that_generate_output)" $ set +a $ sh -c 'printf "%s\n" "$DATABASE_URL"' someurl


4

Two changes to your current script: don't parse ls; instead rely on the shell's globbing because the files are in a subdirectory, either cd there first and run the loop, or use basename and dirname to pull out the directory and filename portions of the file before adding the prefix. (Note: I also changed your "/Path" to "./Path" as I didn't want to ...


1

find . -name '*.bedgraph' -delete should work. Be careful not to delete anything inadvertently.


0

There are two (three) erase commands: one that's part of the util-linux package that's installed on every non-embedded Linux system, and one (two variants actually) based on Perl. See What's with all the renames? The util-linux command is very basic, but you're in the rare situation where it can do what you want. Replace the first space by space-dash-...


1

Some solutions to your problem without the loop # use bash's mapfile with process substitution mapfile -t arr < <( awk '!x[$0]++' <<<"$list" ) # use array assignment syntax (at least bash, ksh, zsh) # of a command-substituted value split at newline only # and (if the data can contain globs) globbing disabled set -f; IFS='\n' arr=( $( awk '!...


0

If I understand your index generation correctly, then awk '{print 5*(NR-1)+1" "$0}' yourfile > oufile should do it. If you want prettier output, you can use printf instead e.g. $ awk '{printf "%-3d %s\n", 5*(NR-1)+1, $0}' yourfile 1 4.184 4.2648 6 4.2281 4.0819 11 4.2204 4.1676 16 4.0482 4.1683 21 4.0156 4.2895 26 4.4504 5.2369 31 4....


2

awk '!x[$0]++' <<< "$list" | while read -r line; do array[count++]=$line done The array (italic) in this case is a part of the subshell (bold). The $line and $array has a value whilst the subshell is alive, so to speak. Once the subshell finishes, aka dies, the parent (spawner) environment is restored. This includes obliteration of any ...


0

Create an index-column on the original data file, using pr -t -n. Create the index data to be inserted as the new column, with each row of index data indexed by the row number. I used a little bash function to do this below. Join the index column with the data using join. Here's a bash script to demonstrate: #!/usr/bin/env bash # insert-counts.sh cols='/...


1

$ rename 's/^(\d\d)\s*/$1 - /' *.mp3 This will rename all MP3 files that has a double digit at the start of their file names, inserting space-dash-space after the digits. So 01 Track name.mp3 will become 01 - Track name.mp3 Judging from your own attempts, all filenames start with the digit zero, and you appear to want to insert a dash directly after the ...


2

Assuming the perl rename command: You're quite close with the last command. rename 's/(0.) /$1 - /' *.mp3 would work. There's no need to escape the space, they have no special meaning in regular expressions (they do in file names, but that doesn't matter here), and you need parentheses around the part you want to reuse.


1

Does it have to use the rename command? $ ls 01 Track name.mp3 02 Track name.mp3 03 Track name.mp3 $ for a in *.mp3 > do > mv -i "$a" "${a%% *} - ${a#* }" > done $ ls 01 - Track name.mp3 02 - Track name.mp3 03 - Track name.mp3


1

That's the Perl rename, I suppose. Perhaps something like this would work: rename 's/^(\d+) ([^-])/$1 - $2/' [0-9]*.mp3 Match anything starting with numbers, then a space, then something other than a dash. Replace with the numbers, a dash, and the next character. (The rest of the name is not touched.) Explicitly checking for the dash here so repeated ...


12

Your shell code has two issues: The echo should not be there. The variable $i is mistyped as $1 in the destination file name. To make a copy of a file in the same directory as the file itself, use cp thefile thecopy If you insert anything else in there, e.g. cp thefile theotherthing thecopy then it is assumed that you'd like to copy thefile and ...


7

Short and precise < test.ogg tee test{1..100}.ogg or even better do tee test{1..100}.ogg < test.ogg >/dev/null see tee command usage for more help. Update as suggested by @Gilles, using tee has the defect of not preserving any file metadata. To overcome that issue, you might have to run below command after that: cp --attributes-only --...


9

for i in {1..100}; do cp test.ogg "test_$i.ogg" ; done


0

From my experience I use the && and || to reduce an if statement to a single line. Say we are looking for a file called /root/Sample.txt then the traditional iteration would be as follows in shell: if [ -f /root/Sample.txt ] then echo "file not found" else echo "file found" fi These 6 lines can be reduced to a single line: [[ -f /root/...



Top 50 recent answers are included