Searching text string in file & replacing it - linux

There is a file in my home directory named conkyrc now this controls my whole of conky setup & there is a nice internet speed meter on my desktop but the problem is that my OS is on a portable drive that is connected to various different places (thus having various network internet access points) so I have to manually replace the keyword for current access point in this conkyrc file (for instance eth5 for eth3).
Now this action can be done by opening file in any text editor & using search/replace...but for triviality I want to create a simple bash script which asks user for proper access point & auto replaces instances of say eth5 for eth3, eth3 being the current user input.
Also a zenity simplistic gui asking for user input is welcome.

#!/bin/bash
if eth=$(zenity --entry --text="Enter the access point") &&
[[ $eth =~ ^eth[0-9]$ ]]
then
sed -i "s/\<eth[0-9]\>/$eth/" ~/conkyrc
fi

Related

How to access the icon set of the current system theme?

I'm writing a bash script that uses notify-send (from the notify-osd package) to show popup notifications to the user, e.g. if there is an error during runtime. These popups can be decorated with icons, like so:
notify-send -i $icon $headline $message
In order to make my script as lean as possible, I want to use the default icons that come with the system. So far, I have resorted to hard-coded filenames, like so:
icon="/usr/share/icons/Mint-Y/status/48/dialog-error.png"
This is obviously a pretty bad solution, since the icon depends on a theme that may not be installed on a given system (in the example above, it's the "Mint Y" theme). I'm looking for a way to tell my script: "Ask the system for the error icon in the currently-set icon theme, and use that."
The question is: how do I do that?
Don't specify anything like a path. You should be specifying a generic filename fragment, and letting the system search for the best value for you.
So, if there is a size or filetype or something in the filename, drop it, and do not specify the leading path.
Use something like notify-send -i info test or notify-send -i error test ...
Or notify-send -i printer-error test, which will find something like /usr/share/icons/Adwaita/256x256/status/printer-error.png under the hood.

How can I enter text into Launchy and append it to the end of a text file?

I'm a Debian Stretch user coming from Windows. In Windows with the launchy app (also available for Linux), I had a method of entering text into launchy that was then appended to the end of a .txt or .md file.
To do this in Windows, I created a file called note.bat that contained the following:
echo %*>>"C:\collectednotes.md"
I'd make launchy aware of note.bat by adding its containing folder to “Launchy” → “Settings” → “Catalog” and adding filetype *.bat.
From there, I'd launch launchy, type note, hit Tab, enter some text, hit Enter, and then the text would be added to the end of collectednotes.md.
A mostly working process is detailed in my answer below. I'll give the green checkmark answer to anyone that can adjust this process (via note.sh and/or launchy plugin setup detailed below) to appropriately handle all special characters.
This may contain the solution to this question:
Which characters need to be escaped in Bash? How do we know it?
Solved (almost). I'm keeping this question unanswered and will give to whoever completes the remaining ~5%. Steps to get the 95% solution with xfce4-terminal version 0.8.3-1:
Install launchy and launchy-plugins (both are version 2.5-4 for me):
apt-get install launchy
apt-get install launchy-plugins
Open terminal to default location of ~/ and create collectednotes.md:
echo "# Launchy Notes Collected Here" > collectednotes.md
Create note.sh shell script:
echo '#!/bin/sh' > note.sh
Create shell script line 2:
echo ALL_ARGUMENTS='"$#"' >> note.sh
Create shell script line 3:
echo 'echo "$ALL_ARGUMENTS" >> ~/collectednotes.md' >> note.sh
If you open note.sh, it will look like:
#!/bin/sh
ALL_ARGUMENTS="$#"
echo "$ALL_ARGUMENTS" >> ~/collectednotes.md
Make note.sh executable:
chmod +x note.sh
Launch launchy and click the gear icon in upper right for settings. If consistency with launchy for Windows is desired, set launchy Hotkey to Alt+Space. If you receive a keyboard shortcut conflict message as I do on Debian with Xfce, first go to Settings, Window Manager, Keyboard tab, and clear Alt+Space as the shortcut for Window operations menu.
Next in launchy settings, go to Plugins tab and enable the plugin Runner. Click the + button and create a new Runner custom command as follows:
- Name: note
- Program: /home/YOURUSERNAMEHERE/note.sh (launchy does not like Program path of ~/note.sh, so a specific path with username is required)
- Arguments: '$$'
Click the Catalog tab and hit Rescan Catalog just in case. Hit OK to close launchy settings.
Now let's test it.
- launch launchy with Alt+Space or your hotkey
- type note (You may have to wait 10 second or so on first run. You'll know things are as expected when you see the text "note" with a orange/yellow icon containing silhouette of a person.)
- hit Tab
- enter some text (no need for single or double quotes or escapes), e.g.: Remember to donate at least $3 to Josh at Launchy https://www.launchy.net/donate.php
- hit Enter
Now open collectednotes.md to confirm the text was captured.
The remaining issues seem to be dealing with single quotes and double quotes. For example, consider the following note:
I don't know what I'd do without Launchy.
Which results in the following in collectednotes.md:
I dont know what Id do without Launchy.
Or:
Would David Allen like universal text capture from anywhere in Linux? My bet is "yes!"
Results in the following in collectednotes.md:
Would David Allen like universal text capture from anywhere in Linux? My bet is \yes!\
Single quoting and/or double quoting the input to launchy doesn't solve it. Note the launchy Runner custom plugin construction component of '$$' is a piece of this puzzle.
I'll give the answer to anyone that can adjust this process (via note.sh and/or launchy plugin setup) to appropriately handle all special characters. Maybe this would add proper escapes to user input with something like gsub.
The rationale for being exacting regarding proper character handling is that this process is useful for copying and logging random chunks of text from web pages, but if common characters like single quotes are not handled as expected, confidence in the system is much reduced.

AppleScript Replace File

I'd like to write an AppleScript for replacing three system files with ones I've modified. I'd like to do this with an AppleScript instead of manually replacing them because I'll have to replace three files every time there's an OS X update. Specifically, I'll be replacing stock graphics drivers with ones I've modified to support a graphics card which is connected via Thunderbolt. Is it possible to write an AppleScript for replacing one file with another? I ask because I know that when you replace a file, a dialog pops up with three options, and I don't know how to address that.
You can do this with Finder:
set freshFile to choose file
tell application "Finder"
move freshFile to desktop replacing yes
end tell
All you need to do is work out the source and destination paths to completely automate the script.
Many scripters do not like working with Finder, for a variety of reasons. If you want something that is incredibly fast, you would use the do shell script inside of your AppleScript:
do shell script " mv -f ~/Desktop/ArlandaTilUppsala.pdf ~/Documents/Employ.pdf"

How can I use variables set from a terminal shell script in a new terminal window or tab? (OSX)

Environmental Variables or exporting of variables doesn't seem to work for me... I see that many versions of this have been covered, but they seem very specific and i'm getting confused.
I'm creating a my first shell script on OSX 10.9. The script does the following so far:
1) Ask user a series of questions such as server ip address, server port, ssh login user name and other variables.
2) Using the variables the user set, it then connects to a linux box (ubuntu 14) via ssh.
3) Once the ssh connects, the terminal launches a program called iperf3 to run on the server using the ip and port specified by the user from the script.
So far all of these steps work so far.
The problem is that once the iperf program begins listening on that port specified by the script, all terminal commands are halted. In order to run iperf3 on my client, at this point, I need to open a new tab or window, and run the commands manually. I would rather have the new tab continue off of the script.
However, even if I run the commands manually, these new tabs or windows in terminal do not know the variables that the user had set before hand from the script. I've been trying the export command and looking into setting environmental variables but i'm rather confused and lost.
The next steps would ideally run from the one script file I created and proceed to do as follows:
4) Create new tab and launch iperf3 using the ip and port specified by the user.
5) Upload the --json file created to a MySQL database running on the linux box.
iperf, for those who do not know, tests the network bandwidth between a client and host with results posted to the terminal. I need to test bandwidth at many different places wirelessly and automating most of the "copy paste" commands will save a lot of time.
Any help would be greatly appreciated.
I know two tricks about passing data between scripts:
a) using a little file containing the data and then get it using:
to write:
echo $ip >> tmpfile
echo $other_data >> tmpfile
to get it back:
var=$(cat tmpfile)
# You'll get one value per line and you can use it with something like
while read line
do
##do some job...
done < tmpfile
b) using '&' at the end of the line to call a script that you don't want to wait, so you don't need another tab or script...
for further reference:
http://www.tldp.org/LDP/abs/html/index.html

Qt - how to detect whether the application is running on GNOME or KDE?

I was wondering how I could do something like this (source) using Qt. I looked through the documentation but couldn't find any method to check if an external process is running.
if [ "$(pidof ksmserver)" ]; then
echo "KDE running."
# KDE-specific stuff here
elif [ "$(pidof gnome-session)" ]; then
echo "GNOME running."
# GNOME-specific stuff here
elif [ "$(pidof xfce-mcs-manage)" ]; then
echo "Xfce running."
# Xfce-specific stuff here
fi
Normally you shouldn't do this. Generally, if your application behaves differently depending on desktop environment, that will be a nasty surprise for any user that switches between them.
The alternative
Use DE-agnostic commands like xdg-open. Advantages:
You don't have to write the logic yourself (xdg-utils already has done this)
More user-friendly. It follows the user's actual preferences; many users use one DE but prefer some applications from a different DE.
Supports other DEs like XFCE, LXDE, Unity, etc..
For example, instead of opening a URL in Firefox or Konqueror according to the currently-running DE, pass the URL to xdg-open to open it in the user's preferred application. (The user might be a Chromium user.) Don't hard-code nautilus or dolphin for GNOME and KDE; instead open the path using xdg-open.
Similarly, for other forms of interaction with the DE, try to use Freedesktop specifications, rather than trying to guess what DE is running. Standards exist for moving files to the trash, adding system tray applets, and adding files to the Recent Files list, among others.
Use QProcess to run pidof foo, then check its stdout? If this is not what you want, search /proc/.
I believe the correct way to do what pidof does is to look at entries in /proc. There's another thread on this here: Find PID of a Process by Name without Using popen() or system()

Resources