Log in as another user and then exit after finishing the desired work in shell script

Today I met a problem that I tried to log in as another user and them execute my script. Simply using su user or su - user will not work or exit in scripts even if I write exit after that.

I want to exeute himawaripy program for a dynamic wall paper. Using crontab does not work.

The fist solution is using sudo -u user command. This works in that the script exit correctly but the wallpaper does not change.

The second solution is found online:


!#/bin/bash
su - user<<!
command_to_execute
exit
!

This also successfully exit but does not change the wall paper.

I believe it’s the program’s fault instead of the shell’s. ==Still finding why. ==

About the usage:
https://en.wikipedia.org/wiki/Here_document

2017/3/19 20:40

Using property files in Java

Java use class Property to assemble a property file. In order to use it, a File class object or FileOutputStream class or FileInputStream class object is required.==(this means the latter two will be read only or write only)==

In order to use it, first call the load() method to load the file or the Stream. ==The steam should not be closed until finish using the file!==

After that, the property file can be easily used pretty much like a hash map and information can be stored or read in the form of key and object.

2017/3/19 18:56

Weird resource release while calling a function in C++

I overload a operator for the Communicater class like this:



friend bool Communicater::operator << (Communicater c, char a); //重载<<

bool operator << (Communicater c, char
a){

return true;
}

*communicater << “override test2”;


But when I call it, I found that a socket inside the Communicater class unespectingly closed right after the call.

With further debug, I believe its because the WSACleanup() which is in the deconstructor of that class executed and further more close the socket.

I think it’s because using formal parameters Communicater c like this bool operator<<(Communicater c, char *a) will actually bring a copy of the actual parameter, passed with pointer *communicater, which is previously in the heap, into the stack. After calling the function, the copy in the stack deconstructs which clean up all WSA data. ==This is different from Java.==That’s why the socket will be closed unespectingly.

At last, by changing the function into friend bool Communicater::operator << (Communicater &c, char *a); which simply reference the object instead of copying it. Problem solved.

2017/3/15 16:10

Later I found that wrong.

In order to make sure continous operator use works the same, I tried just change it to
friend Communicater Communicater::operator << (Communicater &c, char *a);

and get the same error which that the Communicater &c is still destructed even if it suppose to be working. ==Not sure why.====(Now I get it. It’s still copied…2017/3/15 18:32)==

I tried friend Communicater* Communicater::operator << (Communicater &c, char *a); but this way won’t work either because I need to overwrite for another scenario which is Communicater operator << (Communicater &c, char *a);.

At last, I change it to friend Communicater &Communicater::operator << (Communicater &c, char *a); so that it finally works…

2017/3/15 16:30

method ID not in [0, 0xffff]: 65536 error

While an error like this exist, there are simply too many methods in one of the dependencies. Multidex need to be enabled because one dex file can not contain them all.
com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

What google suggest:
https://developer.android.com/studio/build/multidex.html

what I do:


android {
defaultConfig {

multiDexEnabled true
}

}

dependencies {
compile ‘com.android.support:multidex:1.0.0’
}

==Credits goes to @Huy Nguyen on StackOverflow==

2017/2/14 14:43

A fast way to save complex terminator layout into config file and set it to default

It’s quite annoying to manually write the layout file for terminator especially when there are tenth of terminals in the layout. There is a easier way provided by the software itself:

  1. Make the terminal the way I want.
  2. Go to preference-layout-new
  3. The layout file will be automatically generated according to the current layout of the window.

The file can be found under $HOME/.config/terminator/. Name to default inside the layout bar by double clicking it and restart the terminator will make it the default profile–another bug property provided by the program.

2016/3/2 15:54

“NO MATCHING ALGO KEX” error while attempt to ssh via older ssh clients

This error occurs because some older key algorithms are no longer supported by newer servers in that it’s considered insure. ==If possible, upgrade the client instead of using the method below.==

A way to force the server(openssh at here) to use it:

1.Adding KexAlgorithms diffie-hellman-group1-sha1 to /etc/ssh/sshd_config. For example:


sudo echo “KexAlgorithms diffie-hellman-group1-sha1” >> /etc/ssh/sshd_config

2.Restart the ssh server:


sudo service ssh restart

2017/2/27

Downgrade kindle paper white1 (PW) from the system firmware version 5.6.1.1 for jailbreak

==From: https://www.mobileread.com/forums/showthread.php?t=264432 ==

The firmware 5.6.1.1 is considered unbreakable and there is not yet a effective jailbreak for it besides using ttl serial which requires the whole device to be opened up.

Now, through research, there is finally an effective way to perform that. First downgrade the current firmware and then it’s much easier to perform all kinds of jailbreaks.

As I quote:

How to Downgrade Paperwhite 1 Canada from 5.6.1.1 to 5.3.3

Download earlier update file from Amazon: Kindle Paperwhite 1 Update 5.3.3
https://s3.amazonaws.com/G7G_Firmwar...ndle_5.3.3.bin
Disable wifi on your Paperwhite 1 (airplane mode).
Connect your Kindle Paperwhite 1 to your computer (DO NOT DISCONNECT UNTIL LAST
STEP!).
Copy the bin file you downloaded in step 1 to root folder of PW1.
Wait at least 2 minutes after copy has completed.
Push and hold power button until your PW1 restarts (the led blinks orange and
light turns on at the screen).
Wait until the PW1 has installed the upgrade (really a downgrade).
Now you can DISCONNECT from your computer.

Tested and working on Paperwhite 1 I assume it should work on Paperwhite 2.

Original article:
http://blog.the-ebook-reader.com/201...comment-143579
All credits goes to David!

This article is put here just for the purpose of backup and for myself to reference it in the future. Contact me if it’s inappropriate. Thank you.
2016/2/16 20:41

About getline() method in Java

Recently, I was trying to use an example program in Java to communicate with another C++ program through net socket. The example use a method in Class Stream called getline() to read from the socket with cause a problem that I failed to receive anything which is already sent.

There are two reasons:

First, this method will stuck the thread until a line is read. This disable all the debug output in my program.

Second, the method will only return if a \n sign or /r sign is read which is not contained in what’s sent. This disable the method from outputting anything and also stuck it forever.

2017/2/20 12:33