Posts

Showing posts with the label Bash

Count Number Of Lines In A Git Repository

Answer : xargs will do what you want: git ls-files | xargs cat | wc -l But with more information and probably better, you can do: git ls-files | xargs wc -l git diff --stat 4b825dc642cb6eb9a060e54bf8d69288fbee4904 This shows the differences from the empty tree to your current working tree. Which happens to count all lines in your current working tree. To get the numbers in your current working tree, do this: git diff --shortstat `git hash-object -t tree /dev/null` It will give you a string like 1770 files changed, 166776 insertions(+) . If you want this count because you want to get an idea of the project’s scope, you may prefer the output of CLOC (“Count Lines of Code”), which gives you a breakdown of significant and insignificant lines of code by language. cloc $(git ls-files) (This line is equivalent to git ls-files | xargs cloc . It uses sh ’s $() command substitution feature.) Sample output: 20 text files. 20 unique files. 6 fi...

Access Host's Ssh Tunnel From Docker Container

Answer : Using your hosts network as network for your containers via --net=host or in docker-compose via network_mode: host is one option but this has the unwanted side effect that (a) you now expose the container ports in your host system and (b) that you cannot connect to those containers anymore that are not mapped to your host network. In your case, a quick and cleaner solution would be to make your ssh tunnel "available" to your docker containers (e.g. by binding ssh to the docker0 bridge) instead of exposing your docker containers in your host environment (as suggested in the accepted answer). Setting up the tunnel: For this to work, retrieve the ip your docker0 bridge is using via: ifconfig you will see something like this: docker0 Link encap:Ethernet HWaddr 03:41:4a:26:b7:31 inet addr:172.17.0.1 Bcast:172.17.255.255 Mask:255.255.0.0 Now you need to tell ssh to bind to this ip to listen for traffic directed towards port 9000 via ssh -L 1...

Adding Git Credentials On Windows

Answer : Ideally, you should enter: git config --global credential.helper manager-core Then your password would be stored in the Windows Credential Manager. See more at "Unable to change git account". On the first push, a popup will appear asking for your credentials (username/password) for the target server (for instance github.com ) If not, that might means your credentials were already stored. If they are incorrect, a simple git credential-manager reject https://github.com will remove them (on Windows, again. On Mac: git credential-osxkeychain erase https://github.com ) With Git 2.29 (Q4 2020), the parser in the receiving end of the credential protocol is loosen to allow credential helper to terminate lines with CRLF line ending, as well as LF line ending. See commit 356c473 (03 Oct 2020) by Nikita Leonov ( nyckyta ). (Merged by Junio C Hamano -- gitster -- in commit 542b3c2, 05 Oct 2020) credential : treat CR/LF as line endings in the credential protocol...

Add Android-studio/bin/ To PATH Environmental Variable

Answer : It looks like you edited this code snippet: if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH" fi which is included in ~/.profile by default. The answer which lead you to do so is confusing IMNSHO. I'd suggest that you change that code back to what it looked like before, and instead add a new line underneath it: if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH" fi PATH="$PATH:/usr/local/Android/android-studio/bin" Then, next time you log in, PATH ought to be altered, whether $HOME/bin exists or not.

Creating Classes And Objects Using Bash Scripting

Image
Answer : You can try to do something like this example.sh #!/bin/bash # include class header . obj.h . system.h # create class object obj myobject # use object method myobject.sayHello # use object property myobject.fileName = "file1" system.stdout.printString "value is" system.stdout.printValue myobject.fileName obj.h obj(){ . <(sed "s/obj/$1/g" obj.class) } obj.class # Class named "obj" for bash Object # property obj_properties=() # properties IDs fileName=0 fileSize=1 obj.sayHello(){ echo Hello } obj.property(){ if [ "$2" == "=" ] then obj_properties[$1]=$3 else echo ${obj_properties[$1]} fi } obj.fileName(){ if [ "$1" == "=" ] then obj.property fileName = $2 else obj.property fileName fi } system.h . system.class system.class system.stdout.printValue(){ echo $($@) } system.stdout.printString(){ echo $@ } Link for refer...

Conversion Hex String Into Ascii In Bash Command Line

Answer : This worked for me. $ echo 54657374696e672031203220330 | xxd -r -p Testing 1 2 3$ -r tells it to convert hex to ascii as opposed to its normal mode of doing the opposite -p tells it to use a plain format. This code will convert the text 0xA7.0x9B.0x46.0x8D.0x1E.0x52.0xA7.0x9B.0x7B.0x31.0xD2 into a stream of 11 bytes with equivalent values. These bytes will be written to standard out. TESTDATA=$(echo '0xA7.0x9B.0x46.0x8D.0x1E.0x52.0xA7.0x9B.0x7B.0x31.0xD2' | tr '.' ' ') for c in $TESTDATA; do echo $c | xxd -r done As others have pointed out, this will not result in a printable ASCII string for the simple reason that the specified bytes are not ASCII. You need post more information about how you obtained this string for us to help you with that. How it works: xxd -r translates hexadecimal data to binary (like a reverse hexdump). xxd requires that each line start off with the index number of the first character on the line (run hexdump on som...

Cp: Silence "omitting Directory" Warning

Answer : The solution that works for me is the following: find -maxdepth 1 -type f -exec cp {} backup_1364935268/ \; It copies all (including these starting with a dot) files from the current directory, does not touch directories and does not complain about it. Probably you want to use cp -r in that script. That would copy the source recursively including directories. Directories will get copied and the messages will disappear. If you don't want to copy directories you can do the following: redirect stderr to stdout using 2>&1 pipe the output to grep -v script 2>&1 | grep -v 'omitting directory' quote from grep man page: -v, --invert-match Invert the sense of matching, to select non-matching lines.

Creating A New User And Password With Ansible

Answer : I may be too late to reply this but recently I figured out that jinja2 filters have the capability to handle the generation of encrypted passwords. In my main.yml I'm generating the encrypted password as: - name: Creating user "{{ uusername }}" with admin access user: name: {{ uusername }} password: {{ upassword | password_hash('sha512') }} groups: admin append=yes when: assigned_role == "yes" - name: Creating users "{{ uusername }}" without admin access user: name: {{ uusername }} password: {{ upassword | password_hash('sha512') }} when: assigned_role == "no" - name: Expiring password for user "{{ uusername }}" shell: chage -d 0 "{{ uusername }}" "uusername " and "upassword " are passed as --extra-vars to the playbook and notice I have used jinja2 filter here to encrypt the passed password. I have added below tutorial related to this to my ...

Can I Export A Variable To The Environment From A Bash Script Without Sourcing It?

Answer : Is there any way to access to the $VAR by just executing export.bash without sourcing it ? Quick answer: No. But there are several possible workarounds. The most obvious one, which you've already mentioned, is to use source or . to execute the script in the context of the calling shell: $ cat set-vars1.sh export FOO=BAR $ . set-vars1.sh $ echo $FOO BAR Another way is to have the script, rather than setting an environment variable, print commands that will set the environment variable: $ cat set-vars2.sh #!/bin/bash echo export FOO=BAR $ eval "$(./set-vars2.sh)" $ echo "$FOO" BAR A third approach is to have a script that sets your environment variable(s) internally and then invokes a specified command with that environment: $ cat set-vars3.sh #!/bin/bash export FOO=BAR exec "$@" $ ./set-vars3.sh printenv | grep FOO FOO=BAR This last approach can be quite useful, though it's inconvenient for interactive use sin...

Convert Text File Of Bits To Binary File

Answer : Adding the -r option (reverse mode) to xxd -b does not actually work as intended, because xxd simply does not support combining these two flags (it ignores -b if both are given). Instead, you have to convert the bits to hex yourself first. For example like this: ( echo 'obase=16;ibase=2'; sed -Ee 's/[01]{4}/;\0/g' instructions.txt ) | bc | xxd -r -p > instructions.bin Full explanation: The part inside the parentheses creates a bc script. It first sets the input base to binary (2) and the output base to hexadecimal (16). After that, the sed command prints the contents of instructions.txt with a semicolon between each group of 4 bits, which corresponds to 1 hex digit. The result is piped into bc . The semicolon is a command separator in bc , so all the script does is print every input integer back out (after base conversion). The output of bc is a sequence of hex digits, which can be converted to a file with the usual xxd -r -p . Output: $ hexdump -C...