Tag Info

Hot answers tagged

7

The only major difference is between sourcing and executing a script. source foo.sh will source it and all the other examples you show are executing. In more detail: ./file.sh This will execute a script called file.sh that is in the current directory (./). Normally, when you run command, the shell will look through the directories in your $PATH for an ...


7

In Bash: colors="blue blue red green" i=1 for c in $colors; do echo -n "color.$((i++))=$c " done Command-line version (easy to copy&paste to terminal) with 2 lines - setting variables first and then main script: colors="blue blue red green"; i=1 for c in $colors; do echo -n "color.$((i++))=$c "; done


4

Here is a Python way: #!/usr/bin/env python2 input_string = 'blue blue red green' count = 1 for value in input_string.split(): print 'color.{0}={1}'.format(count, value), count += 1 Output: color.1=blue color.2=blue color.3=red color.4=green Here is a sed way: $ n=1;x=color;echo "blue blue red green" | sed -rn "s/^([^ ]+) ([^ ]+) ([^ ]+) ([^ ...


4

Your awk is not sorting. The output is printed in the order it is found in file2 and that is sorted. To keep the order found in file1, just inverse it (I also removed the needless a[$1]"\t"$2, you were already saving $0 in a): $ awk -F "\t" 'FNR==NR{a[$1]=$0; next}$1 in a {print a[$1]}' file2 file1 03873 0.852 03872 0.326 03871 0.852 03870 ...


4

You can try cmp. It will compare two files byte by byte. From man cmp: cmp - compare two files byte by byte Although the number of lines must be equals on two files. Also note that cmp will point to the first difference only, to point to the next differences you can skip specific bytes from the start. $ cat foo this is a test $ cat bar this is a test ...


3

You need to compile it first: first change the current working directory of your Terminal to the path of your source file: cd <path_to_cpp_file>/ Then compile it: g++ myProg.cpp -o myProg Then you can call the compiled executable from your bash script like this: #!/bin/sh # ... <path_to_compiled_executable>/myProg # ...


3

You need to change the quotes around the grep command for backticks: for i in `grep -l matchpattern "$firstdir"/*`; do Or adopt the new-style $(): for i in $(grep -l matchpattern "$firstdir"/*); do Source: What's the difference between $(stuff) and `stuff`?


3

When writing scripts, if you have an error, you need to echo your variables to understand what is happening. This is the first step in debugging. Had you done so, you would have seen that the grep command is not being executed and is instead being saved as $i: $ for i in 'grep -l matchpattern $firstdir/*'; do echo "i is: $i"; done i is: grep -l ...


3

Here is awk solution if your colors are so more: awk '{ printf("color.%d=%s ",NR, $0 ) }' RS='[[:blank:]]+' infile color.1=blue color.2=blue color.3=red color.4=green RS defines Spaces/Tabs as Record Separator with occurrences of one or more times +. Then the printf command print the current Number of Record/field and next print the whole record/field $0 ...


2

I've made this: #!/bin/bash ## Restores the screen when the program exits. trap "tput rmcup; exit" SIGHUP SIGINT SIGTERM ## Saves the screen contents. tput smcup ## Clears the screen. clear ## Loop through available interfaces. while read interface; do # While reads a line of the output i=$((i+1)) ...


2

Suppose you have the following files in the current directory: a/sm1, with the content "a stock market b" b/sm2, with the content "x stock market y" sm3, that does not contain "stock market" destination, a directory where you want to move files containing "stock market". Let's find all the files (of type f = file) in the current directory ( . ): $ find ...


2

Presumably you want %b. From help printf: In addition to the standard format specifications described in printf(1), printf interprets: %b expand backslash escape sequences in the corresponding argument And: $ printf "%b\n" '\101' A I don't know if it works for Unicode characters in general.


2

With the awk commands in the link you post, you'd get something like this: awk '{printf("%d\n",$0+0.5)}' file Or simpler, use: awk '{printf("%.f\n",$0)}' file I can come up with nothing easier than that ;)


2

You didn't give much more information so I'm assuming you have your string in a variable called YOUR_STRING. This can easily be adapted to process files and whatnot. First determine half the length of your string, and add 1 (otherwise you end up with 3 lines and not 2): CHARS=$((`echo $YOUR_STRING | wc -c` /2 + 1)) Next, use the fold command to wrap your ...


1

Probably the only way that hasn't been mentioned is the way i've been using it for years: echo $PATH | tr ":" "\n" so, in your .profile or .bash_profile or whatever, you can add: alias path='echo $PATH | tr ":" "\n"'


1

You can use the following perl oneliner: perl -i -pe 's/(\d*\.\d*)/int($1+0.5)/ge' file The -i option will automatically change your decimal numbers in-place. The regex \d*\.\d* will ensure that only such numbers will be changed in your original file (i.e other strings will be left untouched)


1

You can try with vimdiff: vimdiff -b file1 file2


1

You are not actually pausing to read user input anywhere. echo "You must type 'soff' to end this script." while [ -f .running ] do if read -t 10 -p "I'm still running, type 'soff' to stop me :)... " ans then if [[ $ans == "soff" ]]; then break fi fi done signoff # shouldn't this be "soff" ? read -t returns a failure ...


1

I think some plain bash magic might do the trick: #!/bin/bash dir1="" dir2="" dir3="" shopt -s nullglob for i in *.html) do if [ "$(grep 'keyword1' $i)" != "" ]; then mv -vf "$i" "$dir1" elif [ "$(grep 'keyword2' $i)" != "" ]; then mv -vf "$i" "$dir2" elif [ "$(grep 'keyword3' $i)" != "" ]; then mv -vf "$i" "$dir3" ...


1

Here are a few ways of doing what you want: find find . -iname '*html' -type f -exec grep -q election "{}" \; -and -exec mv {} politics/ \; Explanation Here, we are using find's -exec option: -exec command ; Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until ...


1

The descriptions of the options are all given in man dh_make: Single binary (s) The package will generate a single binary .deb package. It is the standard case, so if you don't know what to do, choose this. Arch-Independent (i) The package will generate a single package that is arch- independent. ...


1

If you don't want to see notifications. Open the activities overview and look for notifications by typing it. And then click at Notifications panel and switch Show Pop up Banners to OFF. Good luck


1

Another perl way: perl -pe 's/SALT/`cat salt.txt`/e' wp-config.php > result.txt The key here is the /e regexp option allowing us to use a perl command result as a substitution string.



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