Posts

Showing posts with the label Shell

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...

Create Django Super User In A Docker Container Without Inputting Password

Answer : Get the container ID and run the command. docker exec -it container_id python manage.py createsuperuser I recommend adding a new management command that will automatically create a superuser if no Users exist. See small example I created at https://github.com/dkarchmer/aws-eb-docker-django. In particular, see how I have a python manage.py initadmin which runs: class Command(BaseCommand): def handle(self, *args, **options): if Account.objects.count() == 0: for user in settings.ADMINS: username = user[0].replace(' ', '') email = user[1] password = 'admin' print('Creating account for %s (%s)' % (username, email)) admin = Account.objects.create_superuser(email=email, username=username, password=password) admin.is_active = True admin.is_admin = True admin.save() else: print(...

Copy And Overwrite A File In Shell Script

Answer : Use cp -fr /source/file /destination this should probably solve the problem. This question has been already discussed, however you can write a little script like this: #!/bin/bash if [ ! -d "$2" ]; then mkdir -p "$2" fi cp -R "$1" "$2" Explaining this script a little bit #!/bin/bash : tells your computer to use the bash interpreter. if [ ! -d "$2" ]; then : If the second variable you supplied does not already exist... mkdir -p "$2" : make that directory, including any parent directories supplied in the path. Running mkdir -p one/two/three will make: $ mkdir -p one/two/three $ tree one one/ └── two └── three If you don't supply the -p tag then you'll get an error if directories one and two don't exist: $ mkdir one/two/three mkdir: cannot create directory ‘one/two/three’: No such file or directory fi : Closes the if statement. cp -R "$1" "$2" : copies files from the first...

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 ...

Alternative Console Host For Windows 7/Windows Server 2008

Answer : Below are some nice console-replacements products that are more user-friendly than cmd. As commented below, since Windows 7, all these shells are just an interface to conhost.exe, even powershell. For details, read What is conhost.exe and Why Is It Running. Therefore, the consoles below only replace the default visual interface to conhost which is the one exhibited by cmd, and are only useful when directly invoked as programs. They cannot be indirectly invoked, as when a console-executable such as diskpart is run, since this will invoke conhost, and conhost has its own I/O interface and API. Here is what Microsoft says in Windows 7 / Windows Server 2008 R2: Console Host : ConHost represents a permanent change in the way that console application I/O is handled. There is no registry key or group policy setting that can force Windows to revert back to “legacy mode” console behavior. The conclusion is that if you wish to replace the console in a deeper way t...

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...