Critics welcome a little Bash Script
#!/bin/bash
new=`kdialog --inputbox "Whats the Name of the DataBase?"`
echo "CREATE DATABASE $new;" > new
echo "USE $new;" >> new
name="total"
function data {
multi=`kdialog --combobox "Select Data type;" "VARCHAR(100)" "INT" "TEXT" "BINARY" "VARBINARY" "TINYBLOB" "TINYTEXT" "BLOB" "MEDIUMBLOB" "MEDIUMTEXT" "LONGBLOB" "LONGTEXT"`
function create {
dat=`kdialog --inputbox "Title of First Data Field for Table $name" "total"`
data
echo "CREATE TABLE $name (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, $dat $multi, cur_timestamp TIMESTAMP(8));" >> new
add=`kdialog --radiolist "Select Number of Additional Fields For $name:" 0 "Zero" on 1 "One" off 2 "Two" off 3 "Three" off 4 "Four" off 5 "Five" off 6 "Six" off 7 "Seven" off`
function mod {
if [ $add -gt '0' ]
then
while [ $add -gt 0 ]
do
col=`kdialog --inputbox "Column Heading for Table $name;"`
echo $col
data
echo "alter table $name add column $col $multi;" >> new
add=`expr $add - 1`
done
fi
echo "show columns FROM $name;" >> new
function extra {
mark=`kdialog --radiolist "Number additional Tables for $name:" 0 "Zero" on 1 "One" off 2 "Two" off 3 "Three" off 4 "Four" off 5 "Five" off 6 "Six" off 7 "Seven" off`
#echo $name
while [ $mark -gt 0 ]
do
name=`kdialog --inputbox "Extra Table Heading;"`
create
mod
mark=`expr $mark - 1`
title="|--| $name"
echo $title
mark2=`kdialog --radiolist "Number additional Tables for $name:" 0 "Zero" on 1 "One" off 2 "Two" off 3 "Three" off 4 "Four" off 5 "Five" off 6 "Six" off 7 "Seven" off`
while [ $mark2 -gt 0 ]
do
name=`kdialog --inputbox "Extra Table Heading;"`
create
mod
mark2=`expr $mark2 - 1`
title="-|- $name"
echo $title
done
done
title="|**| $name"
echo $title
create
name="incoming"
create
mod
title="|**| $name"
echo $title
extra
name="outgoing"
create
mod
title="|**| $name"
echo $title
extra
ans=`kdialog --password "Enter Password:" "******"`
#echo $ans
mysql -u root -p $ans < new
As the title suggests, I'm learning bash a little, now this creates 3 base tables in a mysql database with an optional 2 deep, maximum 7 tables for 2 of the base tables(I'm also looking at a way to link them further on down the line) using KDE kdialog boxes,
My first one is I cant seem to pass the variable to the mysql command, is this possible?
Also I'm missing error trapping, was hoping for a little feed back how I should tackle this(This is an area I need to work on)?
Thanks for the input guys.
Hadn't realised the back tick was on its way out looks like I've got to change a few more things than I thought, easier now to break the habit rather than later.
ans=$(kdialog --password "Enter Password:" "******"); would this be the correct syntax then? I also guess using this will give me the variable $? which I presume I could then use for error trapping. Like this
if [ $? = '0' ]
then
else
fi
Would this be the correct way, though presumably the neater way would be to wrap it in a function?
Makes sense when you got rid of the second echo with a new line.
Have thought about using a tmp file to hold the passwd just didn't really want to do it though. Also not quite sure how I'd amend it to the mysql command. Though thinking about it I presume I could make that earlier and place it at the beginning of the file being read in(Presuming it would work).
Similar Messages
-
A little bash scripting help..
Hello, this is a script I made in /etc/rc.d/sd to mount my SD card as my home partition in my eee pc.
Here's the script:
http://pastebin.ca/1807492
The script works, but I keep getting this error.
[root@UpEarly coda]# /etc/rc.d/sd start
:: Mounting SD card... [BUSY] Waiting for SD card to exist...
Card exists!
/etc/rc.d/sd: line 20: [: too many arguments
Mounted at /home/coda
[DONE]
What's wrong with the if statement at line 20?
Last edited by DeeCodeUh (2010-02-24 00:00:54)perbh wrote:Other than that - I've always had problems with
if test .... and if [ ... ], so these days if I just want to check if true or false I use '&&' or '||'.
If I want to handle both cases, I do 'if test $? -eq 0; then ...; else ...; fi'
Maybe this will help ... "test" and "[ ]" are synonymous really.
test
Evaluate a conditional expression expr. Each operator and operand must be a separate argument.
When the [ form is used, the last argument to the command must be a ].
In other words... you only use test or [] in your "if" statement if your comparing one thing to another, and that will "evaluate" as being either true or false... BUT if your just checking to see if blah blah blah is true... then it's not necc to use those "test" or "[ ]".
Regarding this if statement:
if cat /proc/mounts | grep -q $MNTPNT;
Easiest way for me to remember when to use this is if you just run the command on the command line without the "if" and it returns "something", that means it is "true", if it runs and you get nothing back.. it's returning as "false".
Example: you can't very well compare/run "x = b" on the command line and have it return anything...so it has to be "evaluated"...that's where "test" or "[ ]" come into play.
But using "test" or "[ ]" to check if x does equal b, and if x does equal b it does returns true, and if x doesn't equal b it returns false.
Anyway...that's how I remember it
Last edited by crouse (2010-02-24 04:58:03) -
Bash script to sync KeepassX database
I created a my first little bash script to sync my Keepass(X) (Keepass on Windows boxes and KeepassX on Linux boxes) over a secure (SSL + Login) webdav enabled site.
#!/bin/bash
host=https://www.some_dav_enabled_website.com/db.kdb
user=foo
pass=bar
fpath=/somefolder/db.kdb
function update {
echo "Deleting old KeepassX database..."
rm $fpath
echo "Downloading new KeepassX database..."
curl -sku $user:$pass $host -o $fpath
echo "Starting KeepassX..."
keepassx $fpath &
function sync {
echo "Uploading the KeepassX database to server..."
curl -sku $user:$pass -X PUT -T $fpath $host &> /dev/null
echo "Database updated!"
while getopts "us" optname
do
case "$optname" in
"u")
update
"s")
sync
echo "Usage: -u Update to the latest KeepassX database."
echo " -s Sync the (un)modified KeepassX database."
esac
done
Enjoy !
Last edited by SiB (2008-07-31 23:58:59)Hi Sybrand Bakker,
I tried streams for the replication purpose as per your suggesion, till Iam unable make it work , i dont find a step by step document which will make it possible without error.
One more thing , i need the streams work without Database link, ie., source database is not connected directly to destination database. I need to create streams and transfer the stream as a file through FTP.I need to download stream file in remote location and then apply the streams to destination database and after this source and destination database should be same in data and schema.
Please suggest me a solution to go abt this scenario. We are in critical stage to make it happen...
thanking you in advance
with regards
vivek
Message was edited by:
Vivekanandh -
[SOLVED] Bash scripts to mount & unmount optical drive in Worker?
I'm running XFCE on Arch with the HAL daemon being called in /etc/rc.conf.
I can access media on my optical drive (DVD's or CD's) through the desktop icon that appears after HAL has recognised the drive, VLC automatically does its thing as does NeroLinux.
The reason I'm posting is that I found a great DOpus clone yesterday called Worker - http://www.boomerangsworld.de/cms/worker/index?lang=en, which I am in the process of configuring.
A problem I have is being able to access the optical drive via Worker.
The way it is on my system, with HAL handling it, the first line (see below) appears after HAL mounts the media, which basically makes the two lines below it useless:
/media/<title of disk>
/media/cd
/media/dvd
I have tried configuring Worker to use /media/dvd (or cd), to access the optical media, these don't work for the reason stated above, & /dev/sd0 doesn't work either.
So, do I have to turn off HAL, uncomment the lines in fstab & use mount?
A little bash script, that would do the job for me would be great, as Worker will accept a script or a command string.
I am a bash baby, so if someone can see a solution please post it?
All input welcome.
Thanks.
Last edited by handy (2008-11-19 04:11:02)Zariel wrote:
i guess something like this?
%optical ALL=(ALL) NOPASSWD: ALL
I found the clues for this in the sudoers manual:
handy ALL = NOPASSWD: /sbin/umount /CDROM,\
/sbin/mount -o nosuid\,nodev /dev/cd0a /CDROM
Which works in so far as now mounting no longer needs the password.
Which leaves me with the problem of trying to understand how to get Worker to mount the optical drive on command.
If I enter the bash command in the Terminal as follows:
mount /mnt/dvd
the media is mounted, after which I can push the button in Worker, which I have configured with:
/mnt/dvd
& the root list of the optical media is displayed in the active panel of Worker.
I just haven't been able to get Worker to use "mount /mnt/dvd" yet, there will be a way, I wonder how long it will take me to find it? lol
Last edited by handy (2008-11-19 06:48:09) -
Bash script to trim all filenames with special characters recursively?
Hi,
I have a 30 GB directory full of data I recovered from a friend's laptop after her Windows XP crashed. I'd like to burn that data, but I can't, because many of the filenames contain weird characters (spaces, accents, things even worse that my XTerm displays as inverted question marks). So, mkisofs exits with an error.
I'd like to clean that mess up, but it would take months to do that manually. Well, I only know a very little Bash, but I think this problem is already too heavy for my modest knowledge. Here's the problem:
- check the contents of directory ~/backup recursively
- find files whose filenames contain characters other than [A-Za-z0-9] and then maybe "-" and "_" and ".".
- replace these characters either by an "_" or just erase them
Now how would I translate that into a little Bash script?
Cheers...Heyyyyy... nice idea
-
Bash script workaround for intel backlight problems
Hi all,
I have LXDE with kernel 2.6.30, xf86-video-intel-newest (2.8 rc) on my Samsung NC10, and like many people I have been unable to change the backlight settings in X with kms turned on.
I have tried the various solutions on the forum and none of them work for me, so here are two little bash scripts that use the setpci command to change the backlight.
Note 1: I have a little file .backlight in my home dir that I store the backlight setting because I don't know how to read the value back from the pci address. So if you use this script you need to do this first:
echo FF > ~/.backlight
Note 2: These scripts also requre bc from extra to do the hex conversion & maths.
sudo pacman -Sy bc
It's only 240 K in size, but if you are trying to keep the number of installed packages low, then you won't like this
Here is the package details from pacman:
Name : bc
Version : 1.06-5
URL : None
Licenses : GPL
Groups : None
Provides : None
Depends On : readline
Optional Deps : None
Required By : None
Conflicts With : bc-readline
Replaces : bc-readline
Installed Size : 240.00 K
Packager : Allan McRae <[email protected]>
Architecture : i686
Build Date : Sat 02 Aug 2008 14:18:04 IST
Install Date : Thu 25 Jun 2009 16:06:53 IST
Install Reason : Explicitly installed
Install Script : No
Description : An arbitrary precision calculator language
bc /usr/
bc /usr/bin/
bc /usr/bin/bc
bc /usr/bin/dc
bc /usr/share/
bc /usr/share/info/
bc /usr/share/info/bc.info
bc /usr/share/info/dc.info
bc /usr/share/man/
bc /usr/share/man/man1/
bc /usr/share/man/man1/bc.1.gz
bc /usr/share/man/man1/dc.1.gz
You also need to know the address of you graphics card:
lspci | grep Display
For me this returns:
00:02.1 Display controller: Intel Corporation Mobile 945GM/GMS/GME, 943/940GML Express Integrated Graphics Controller (rev 03)
This is needed for this command in the scripts:
sudo setpci -s 00:02.1 F4.B=$var3
Note the 00:02.1 is the address for my laptop screen, 00:02.0 is the address for the vga out controller for me.
Increase backlight:
#!/bin/bash
var1=`cat ~/.backlight`
var2=`echo "16 i $var1 F + p"|dc`
if (( ( "$var2" \> "-1") && ( "$var2" \< "256" ) ));
then
var3=`echo "10 i 16 o $var2 p"|dc`
echo $var3 > ~/.backlight
sudo setpci -s 00:02.1 F4.B=$var3
fi
Decrease backlight:
#!/bin/bash
var1=`cat ~/.backlight`
var2=`echo "16 i $var1 F - p"|dc`
if (( ( "$var2" \> "-1") && ( "$var2" \< "256" ) ));
then
var3=`echo "10 i 16 o $var2 p"|dc`
echo $var3 > ~/.backlight
sudo setpci -s 00:02.1 F4.B=$var3
fi
Obviously someone else can write a much smarter/better set of scripts than me, but I hope is can be of use for someone!
Edit:Here are my openbox bindings for the scripts:
<keybind key="XF86MonBrightnessUp">
<action name="Execute">
<command>~/bin/blightu.sh</command>
</action>
</keybind>
<keybind key="XF86MonBrightnessDown">
<action name="Execute">
<command>~/bin/blightd.sh</command>
</action>
</keybind>
Last edited by makimaki (2009-07-11 14:26:08)FYI: The new xf86-video-intel (2.9) has just been released:
http://article.gmane.org/gmane.comp.fre … xorg/40733
According to the release announcement, the new driver fixes the backlight issue with KMS:
* Add support for BACKLIGHT property when using kernel modesetting
(KMS). This allows backlight adjustment with programs such as
"xbacklight -set <percentage>" or "xrandr --set BACKLIGHT <value>". -
Bash script to connect to strongest unencrypted wifi AP
Here's a little bash script that looks at the available wifi APs that your card can see, and automatically connects you to the AP with the highest quality and that is also unencrypted. I'm just posting it in case anyone wants to improve upon it or use it. And yes, my bash knowledge kinda sucks.
Just call it like "wifi.sh wlan0" as root.
#!/bin/bash
# Finds the strongest unencrypted AP and tries to connect to it via dhcp
# Call this script like "wifi.sh wlan0"
interface=$1
iwlist $interface scan > blah
NumAPs=`cat blah | grep ESSID | wc -l`
BestAP=0
BestQuality=-1
for i in `seq 1 $NumAPs`;
do
# Check if AP is encrypted
Encryption=`cat blah | grep Encryption | head -n$i | tail -n1 | cut -d":" -f2`
if [ $Encryption = "off" ]; then
# Find AP with the highest quality
QUALITY=`cat blah | grep Quality | head -n$i | tail -n1 | cut -d":" -f2 | cut -d"/" -f1 | sed 's/ //g'`
if [ $QUALITY -gt $BestQuality ]; then
BestQuality=$QUALITY
BestAP=$i
fi
fi
done
if [ $BestAP -gt 0 ]; then
# Yay, we found an unencrypted AP:
echo Connecting to...
ESSID=`cat blah | grep ESSID | head -n$BestAP | tail -n1 | cut -d""" -f2`
echo ESSID=$ESSID
MODE=`cat blah | grep Mode | head -n$BestAP | tail -n1 | cut -d":" -f2`
echo Mode=$MODE
CHANNEL=`cat blah | grep Channel | head -n$BestAP | tail -n1 | cut -d"(" -f2 | sed 's/Channel //g' | sed 's/)//g'`
echo Channel=$CHANNEL
# Connect
iwconfig $interface essid $ESSID mode $MODE channel $CHANNEL
if [ -e /etc/dhcpc/dhcpcd-${interface}.pid ]; then
rm /etc/dhcpc/dhcpcd-${interface}.pid
fi
dhcpcd $interface
# Cleanup
rm blah
fiFor whatever reason, your quality line uses an equal sign whereas every other line of your scan has a colon (and most people have a colon for every line). Too weird..
Anyway, give this a shot:
#!/bin/bash
# Finds the strongest unencrypted AP and tries to connect to it via dhcp
# Call this script like "wifi.sh wlan0"
TEMP=/tmp/bestap.tmp
LOCK=/var/lock/bestap.lock
if [ `whoami` != "root" ];then
echo "Sorry, you need to be root to run this program"
exit 1
fi
if [[ -z $1 ]];then
echo "USAGE: $0 device"
exit 1
else
interface=$1
fi
# Checking for lock
if [[ -e $LOCK ]];then
exit 1; # Too simply nothing to do here :)
else
touch $TEMP $LOCK
fi
isNotInteger()
x=$1
case $x in
*[!0-9])
return 0 ;;
return 1 ;;
esac
# Proggy
iwlist $interface scan > $TEMP
NumAPs=`cat $TEMP | grep ESSID | wc -l`
BestAP=0
BestQuality=-1
for i in `seq 1 $NumAPs`;
do
# Check if AP is encrypted
Encryption=`cat $TEMP | grep Encryption | head -n$i | tail -n1 | cut -d":" -f2`
if [ $Encryption = "off" ]; then
# Find AP with the highest quality
QUALITY=`cat $TEMP | grep Quality | head -n$i | tail -n1 | cut -d":" -f2 | cut -d"/" -f1 | sed 's/ //g'`
if isNotInteger "$QUALITY"; then
# If we didn't find an integer, try this instead:
QUALITY=`cat $TEMP | grep Quality | head -n$i | tail -n1 | cut -d"=" -f2 | cut -d"/" -f1 | sed 's/ //g'`
fi
if [ "$QUALITY" -gt "$BestQuality" ]; then
BestQuality=$QUALITY
BestAP=$i
fi
fi
done
if [ $BestAP -gt 0 ]; then
# Yay, we found an unencrypted AP:
echo Connecting to...
ESSID=`cat $TEMP | grep ESSID | head -n$BestAP | tail -n1 | cut -d""" -f2`
echo ESSID=$ESSID
MODE=`cat $TEMP | grep Mode | head -n$BestAP | tail -n1 | cut -d":" -f2`
echo Mode=$MODE
CHANNEL=`cat $TEMP | grep Channel | head -n$BestAP | tail -n1 | cut -d"(" -f2 | sed 's/Channel //g' | sed 's/)//g'`
echo Channel=$CHANNEL
# Connect
iwconfig $interface essid $ESSID mode $MODE channel $CHANNEL
if [ -e /etc/dhcpc/dhcpcd-${interface}.pid ]; then
rm /etc/dhcpc/dhcpcd-${interface}.pid
fi
dhcpcd $interface
# Cleanup
fi
rm -f $TEMP $LOCK -
hi,
I am trying to write a little bash script for adding all songs with the same artist as the currently playing song. Here is what I already have:
artist=`mpc -f %artist% | head -n 1`
search=`mpc search artist "$artist" | sed 's|mp3|mp3"|g' | sed 's|ganze|"ganze|g'`
echo $search | mpc add
the sed commands are for enclosing all paths in the list by "
the point is that the last command doesn't work and I don't know why! if I type
echo $search
and copy the the output to the end of mpc add, mpd adds the songs to it's playlist. so my problem here is that mpc doesn't "eat" the content of the search-variable in the pipe. what am I doing wrong????
thx in advancebrisbin33 wrote:
kittykatt wrote:Have you thought of surrounding the $search variable in double quotes in the last line? If there are spaces involved in that variable, then you'll most likely need them.
As far as I know, echo doesn't care about quotes unless you're trying to preserve whitespace at the beginning or end of the line.
# these are equivalent
var='my awesome variable'
echo $var
echo "$var"
# but these you need to be careful
var=' my awesome variable with whitespace '
echo $var
echo "$var"
Touche. It's been a long day for me, unfortunately. Starting to make slip-ups like that. -
Spinach (bash script)
Hello,
The other day I was talking in the IRC channel and mentioned a little script I wrote to download packages from the AUR. Somebody wondered how I would update packages... I hadn't thought that far into the future. However, now I have. I whipped up a little bash script. It does only the basics, but I was wondering if somebody could check it over and see if I'm doing anything horrendous. Sorry, the tabbing is messed up in a few places.
Script: http://floft.net/uploads/spinach/spinach (also in the AUR)
Description: http://floft.net/wiki/Scripts.html
As for the name, I was going to name it "tower" since it's amazingly similar to "cower," but I didn't want to cause any confusion. Thus, I named it after a wonderful food.
Note: The goal was to see how small I could make a functional AUR helper... I know there's a lot of perfectly good ones already.
Garrett
Last edited by Floft (2012-06-28 17:44:30)Odd that you would use both curl and wget. curl proves to be far more useful in the long run, doing everything that wget does (and more). Neat idea though. I've toyed with minimalist bash-based AUR helpers, as well as wrapping cower.
-
Sending email using bash script
Hello:
I am working on writing a bash script to notify one or more users by email of certain events. Run from the Terminal command line, and having the script "echo" text of (what would be) a form letter with in-line variable expansion (i.e., ${VARIABLE}), all seems to work as anticipated. Eventually, I want cron to launch this shell script, and send an email to an "on-subnet" user (I have postfix enabled on my Mac, and there are multiple local user accounts).
I found some stuff on the web about sending mail from bash scripts, and so I made a small little test script, that reads like this:
#!/bin/bash
VARIABLE[1]="The 12,345 quick brown foxes "
VARIABLE[2]="jumped over the 67,890 lazy dogs."
mail -s "a test email" jv << EOF
This is a test:
${VARIABLE[1]}
${VARIABLE[2]}
This is the last line of the test message.
EOF
echo "script completed"
It worked... almost... It sent a local email to my postfix mail account that read like this:
This is a test:
The 12,345 quick brown foxes
jumped over the 67,890 lazy dogs.
This is the last line of the test message.
EOF
echo "script completed"
So, I have two questions. First, the easy one (I hope):
How do I delimit the end of the text, that I want to be the message body of the email, from portions of the script that follow said email text?
Next question is a little more involved. You know how, in Mail.app, if you go to Mail Preferences>Accounts>Account Information, you can put multiple email addresses, comma-delimited, in the "Email Address" field? So, if a person entered "[email protected], [email protected], [email protected]" in this field, then, even though (s)he may be at home, and using their home ISP's mail server, (s)he could send an email apparently from either their home, work, or school email address. Of course, the mail headers clearly would show it came from and through their home machine and home ISP, but it would be displayed in the recipient's Mail client viewer as having come from one of [email protected], [email protected], or [email protected].
I'd like to do something similar here, whereby the email (that is being sent to one or more local users' postfix account on my computer) would apparently be sent from "watchdog@localhost" rather than from "jv@localhost" like it seems to do by default. Whatever account the script is run from (or presumbably, whose cron tab is launching the script) is what the "From" address is set to.
I'd rather not create an additional mail account, because I am using Mac OS X built-in accounts for the postfix mailboxes (I don't want to have to maintain a plaintext username:password file in postfix, and I don't want to create an additional user account on the computer).
So, is there a way to specify an alternate "From" username when invoking the mail -s ${SUBJECT} ${RECIPIENT} command in a bash script? Or is there a different, alternate mail command that will let me do so? (please include a description of syntax and how I'd package the above message text for the alternate method).
Thanks in advance, all!Hi j.v.,
The > after EOF is just a typo (or may be added by the Discussion ?) and you must delete it; other > are prompts from the interactive shell. Andy's post shows an interactive use of shell, not a shell script (note the shell prompt % in front of the commands). A typical use of here document may look like
command <<ENDOFDATA
ENDOFDATA
There must be no spaces before and after ENDOFDATA. The word ENDOFDATA can be EOF or any other string which is guaranteed not to appear in the text (the .... in the example above).
You can modify the From: header by using sendmail command (postfix has it as a compatibility interface):
/usr/sbin/sendmail -t <<EndOfMessage
Subject: test mail
To: jv
From: watchdog
This is a test:
${VARIABLE[1]}
${VARIABLE[2]}
This is the last line of the test message.
EndOfMessage
There must be a blank line between the headers and the mail body.
I assume that you send these mails only to users on your local Mac. Please do not send mails to remote users by using the sendmail command unless you know what you are doing completely.
PowerMac G4 Mac OS X (10.4.5) -
Using Bash script to edit config file
This is a really simple question, but given that I'm just learning Bash scripting and having this solved now would be really illustrative for me, I would really thank some help here.
I'm using uzbl, and running Tor+Polipo. So, as you will see below in the tail of the config file, there is a line to redirect the requests of uzbl through Polipo.
# === Post-load misc commands ================================================
sync_spawn_exec @scripts_dir/load_cookies.sh
sync_spawn_exec @scripts_dir/load_cookies.sh @data_home/uzbl/session-cookies.txt
# Set the "home" page.
#set uri = https://duckduckgo.com
# Local polipo proxy
set proxy_url = http://127.0.0.1:8123
# vim: set fdm=syntax:
What I want to accomplish is to comment in/out that line with a key shortcut on Awesome. I've thought of doing 2 scripts to do so and using 2 differente key shortcuts, but I want to "toggle" the proxy redirection with only 1 shortcut. To do so, I suppose that the script should go something like:
if
tool 'set proxy_url = http://127.0.0.1:8123' config_file
then
tool '#set proxy_url = http://127.0.0.1:8123' config_file
else
if
tool '#set proxy_url = http://127.0.0.1:8123' config_file
then
tool 'set proxy_url = http://127.0.0.1:8123' config_file
fi
fi
I know little about sed, but I think is the tool for this job. The most intriging part to me is to ask sed to print the regular expression when it finds it in the config file, and use that as an input in the conditional statement.
Well, this is a mess I have done here. Hope there is a simple answer to this.
Thanks in advance.-You can do this with a single sed command:
sed -i 's/^#set proxy_url/set proxy_url/;
t end;
s/^set proxy_url/#set proxy_url/;
: end' config_file
This edits the file in-place (-i) and first tries to replace the commented with the uncommented line. If that suceeds, sed jumps to the "end" label. If not, it tries to replace the uncommented with the commented line. Thus you don't have to include any logic about the current state: if the first substitution succeeds, the line was obviously commented, if not, it was uncommented, and the second substitution should succeed.
Note that my knowledge of sed is very limited. There might be a simpler way to do this.
EDIT: For the sake of example, here's how to do the same in bash using regular expressions. Note how this script needs to use a temporary file to simulate in-place editing, how it needs to process the file line by line manually, etc. All things that sed does out of the box...
#!/bin/bash
tmp=test.conf.tmp
echo -n "" > "$tmp"
while read line; do
if [[ "$line" =~ ^#set\ proxy ]]; then
echo "${line/\#/}" >> "$tmp"
elif [[ "$line" =~ ^set\ proxy ]]; then
echo "#$line" >> "$tmp"
else
echo "$line" >> "$tmp"
fi
done < test.conf
mv test.conf.tmp test.conf
To answer your original question, the line
if [[ "$line" =~ ^#set\ proxy ]]; then
reads: if the line begins with a "#", followed by "set proxy", then...
Last edited by hbekel (2011-03-20 10:40:16) -
HOW DO I RUN A UNIX BASH SCRIPT FROM JAVA??
HI. Here's a tricky little problem i have. There's a unix bash script that has some commands in it, that manipulate a file. It appends a certain string variable to a file called users. The users file is an ordinary text file.
I know this script to work perfectly, when i invoke it like this directyl from the command line: ./addusers.sh
or even: bash /downloads/selinux/policy/addusers.sh
Now, i have a java program, and its meant to just execute that script. It doesnt throw any Exceptions at runtime. But when i look at the users file, and expect it to have an extra line that was the string variable, the file is UNTOUCHED!
Again, direct command line invocation works, but not from java. Here's what my invocation from java looks like:
Process p = Runtime.getRunTime().exec("bash downloads/selinux/policy/addusers.sh");
The strange thing is, i tried a different bash command. I tried:
Process p = Runtime.getRunTime().exec("mkdir /temporary");
and this worked!
so why not the other one??
I cant figure it out.You say:
bash /downloads/selinux/policy/addusers.sh
And you say in Java:
Process p = Runtime.getRunTime().exec("bash
downloads/selinux/policy/addusers.sh");
As if a leading / would be missing from the Java
version...nyix says:>
...OK sorry about that. i DO have a / in front of the downloads.... section in the java method. So its:
Process p = Runtime.getRunTime().
exec("bash /downloads/selinux/policy/addusers.sh");
HELP please? -
Bash script to dumpstream many files simultaneously with mplayer
hi guys
i have a problem which i´m sure can be solved with the power of bash scripting
unfortunately i´m no bash scripting guru and all my experiments failed so far
the problem:
i have a file in which are links(streaminglinks)
mplayer offers the funtion to dump such a stream with simply issuing
mplayer -dumpstream mms://path/to/video -dumpfile video1
for example.
now i want mplayer to download this streams specified in the links-file automatically.
basically all it required is a bash script which goes through the link file and generates a command like mplay -dumpstream <link> -dumpfile video<n>
(where n is the nth link) and execute it.maybe there a even simpler solutions
well since i´m not that experienced with bashscripting i can´t solve that problem at my self....
i´m grateful for any helphey guys
thx for the two scripts.
my approach was nearly the same as your´s kraluz with the difference that it doesn´t work
but they both have a little blemish
they download the files sequentially not simultaneously
how could that be realised
thx in advance -
Bash script for checking link status
So I'm doing some SEO work I've been tasked with checking a couple hundred thousand back links for quality. I found myself spending a lot of time navigating to sites that no longer existed. I figured I would make a bash script that checks if the links are active first. The problem is my script is slower than evolution. I'm no bash guru or anything so I figured maybe I would see if there are some optimizations you folks can think of. Here is what I am working with:
#!/bin/bash
while read line
do
#pull page source and grep for domain
curl -s "$line" | grep "example.com"
if [[ $? -eq 0 ]]
then
echo \"$line\",\"link active\" >> csv.csv else
echo \"$line\",\"REMOVED\" >> csv.csv
fi
done < <(cat links.txt)
Can you guys think of another way of doing this that might be quicker? I realize the bottleneck is curl (as well as the speed of the remote server/dns servers) and that there isn't really a way around that. Is there another tool or technique I could use within my script to speed up this process?
I will still have to go through the active links one by one and analyze by hand but I don't think there is a good way of doing this programmatically that wouldn't consume more time than doing it by hand (if it's even possible).
ThanksI know it's been awhile but I've found myself in need of this yet again. I've modified Xyne's script a little to work a little more consistently with my data. The problem I'm running into now is that urllib doesn't accept IRIs. No surprise there I suppose as it's urllib and not irilib. Does anyone know of any libraries that will convert an IRI to a URI? Here is the code I am working with:
#!/usr/bin/env python3
from threading import Thread
from queue import Queue
from urllib.request import Request, urlopen
from urllib.error import URLError
import csv
import sys
import argparse
parser = argparse.ArgumentParser(description='Check list of URLs for existence of link in html')
parser.add_argument('-d','--domain', help='The domain you would like to search for a link to', required=True)
parser.add_argument('-i','--input', help='Text file with list of URLs to check', required=True)
parser.add_argument('-o','--output', help='Named of csv to output results to', required=True)
parser.add_argument('-v','--verbose', help='Display URLs and statuses in the terminal', required=False, action='store_true')
ARGS = vars(parser.parse_args())
INFILE = ARGS['input']
OUTFILE = ARGS['output']
DOMAIN = ARGS['domain']
REMOVED = 'REMOVED'
EXISTS = 'EXISTS'
NUMBER_OF_WORKERS = 50
#Workers
def worker(input_queue, output_queue):
while True:
url = input_queue.get()
if url is None:
input_queue.task_done()
input_queue.put(None)
break
request = Request(url)
try:
response = urlopen(request)
html = str(response.read())
if DOMAIN in html:
status = EXISTS
else:
status = REMOVED
except URLError:
status = REMOVED
output_queue.put((url, status))
input_queue.task_done()
#Queues
input_queue = Queue()
output_queue = Queue()
#Create threads
for i in range(NUMBER_OF_WORKERS):
t = Thread(target=worker, args=(input_queue, output_queue))
t.daemon = True
t.start()
#Populate input queue
number_of_urls = 0
with open(INFILE, 'r') as f:
for line in f:
input_queue.put(line.strip())
number_of_urls += 1
input_queue.put(None)
#Write URL and Status to csv file
with open(OUTFILE, 'a') as f:
c = csv.writer(f, delimiter=',', quotechar='"')
for i in range(number_of_urls):
url, status = output_queue.get()
if ARGS['verbose']:
print('{}\n {}'.format(url, status))
c.writerow((url, status))
output_queue.task_done()
input_queue.get()
input_queue.task_done()
input_queue.join()
output_queue.join()
The problem seems to be when I use urlopen
response = urlopen(request)
with a URL like http://www.rafo.co.il/בר-פאלי-p1
urlopen fails with an error like this:
Exception in thread Thread-19:
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib/python3.3/threading.py", line 639, in _bootstrap_inner
self.run()
File "/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib/python3.3/threading.py", line 596, in run
self._target(*self._args, **self._kwargs)
File "./linkcheck.py", line 35, in worker
response = urlopen(request)
File "/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 160, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 473, in open
response = self._open(req, data)
File "/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 491, in _open
'_open', req)
File "/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 451, in _call_chain
result = func(*args)
File "/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 1272, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 1252, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib/python3.3/http/client.py", line 1049, in request
self._send_request(method, url, body, headers)
File "/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib/python3.3/http/client.py", line 1077, in _send_request
self.putrequest(method, url, **skips)
File "/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib/python3.3/http/client.py", line 941, in putrequest
self._output(request.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 5-8: ordinal not in range(128)
I'm not too familiar with how character encoding works so I'm not sure where to start. What would be a quick and dirty way (if one exists) to get URLs like this to play nicely with python's urlopen? -
I've always been using a python script for this. I found the curl command and decided to learn some bash and parse the output. I'm pretty sure there's a better way of chaining the sed commands but for the life of me I wasn't able to get it to work.Comments and criticism please.
#!/bin/bash
#Script to check gmail
#Needed packages :: curl,sed,grep
#Created by Julian Gagnon
#Setting the variable
username="" #Input Gmail username here.
#OBD (On Bash Display)
clear
echo "###############"
echo "Gmail Bashified"
echo "###############"
echo " "
read -p "Password : " -esr password
echo " "
curl -su "$username":"$password" https://mail.google.com/mail/feed/atom -o /tmp/gmail.out # get the email and stick it in /tmp
#Parse xml file
grep -E "title|tagline|fullcount|summary|issued|name|email" /tmp/gmail.out \
|sed '/<summary><\/summary>/d'|sed '/<link/d'|sed '/<\/fullcount>/ a\ '\
|sed '/<\/email/ a\ '| sed -e :a -e 's/<[^<]*>/ /g;/</{N;s/\n/ /;ba;}'
# Delete temporary email
rm /tmp/gmail.outI've always been using a python script for this. I found the curl command and decided to learn some bash and parse the output. I'm pretty sure there's a better way of chaining the sed commands but for the life of me I wasn't able to get it to work.Comments and criticism please.
#!/bin/bash
#Script to check gmail
#Needed packages :: curl,sed,grep
#Created by Julian Gagnon
#Setting the variable
username="" #Input Gmail username here.
#OBD (On Bash Display)
clear
echo "###############"
echo "Gmail Bashified"
echo "###############"
echo " "
read -p "Password : " -esr password
echo " "
curl -su "$username":"$password" https://mail.google.com/mail/feed/atom -o /tmp/gmail.out # get the email and stick it in /tmp
#Parse xml file
grep -E "title|tagline|fullcount|summary|issued|name|email" /tmp/gmail.out \
|sed '/<summary><\/summary>/d'|sed '/<link/d'|sed '/<\/fullcount>/ a\ '\
|sed '/<\/email/ a\ '| sed -e :a -e 's/<[^<]*>/ /g;/</{N;s/\n/ /;ba;}'
# Delete temporary email
rm /tmp/gmail.out
Maybe you are looking for
-
Customer rebate report - retrieve Invoice numbers
Hi, When I use Vbo3 t-code and enter a rebate number and drill down to Sales volume of rebate management and hit SCOPE button and emty the content I get a list with Invoice numbers. I looked in KONA table it is header and how do we retriev data of th
-
Does anyone know how to pick a random color from a list of colors, say {"red", "green", "yellow", "blue"} ?
-
Dynamic Routing in OSB11g.
I have 3 Proxy services (TestSbServices1,TestSbServices2,TestSbServices3) with protocol as sb and service type as Any XML. I am try to use Dynamic routing to route the input message to one of the above 3 proxy services. My dynamic route action has be
-
How to query the tablespace size increment speed?
Hello, Do you have any directions or ideas on how to know the tablespace size increments history. or how to know the frequency of tablespace size increment. when I went into new database, I never managed this database before, i want to know the tabel
-
How do you make PP CS5 use more allocated physical memory???
As you know, you can go to Preferences > Memory and change the amount of RAM reserved for other applications. Now, I have 8 GB of RAM installed (Windows 7), and I have set my "RAM reserved for other applications" option to 1.5 GB. That leaves 6.5 GB