Bash script "open terminal and launch command
I need a script to do this:
launch a terminal, from this change working directory and then launch a command; the terminal have to remain opened after the command execution.
Something like:
$terminal cd $dir && $command
If I open a terminal and I do:
cd $dir && $command
it works
If I do a .sh script with this:
xterm -hold -e cd $dir && $command
or
terminal -H -x cd $dir && $command
when I launch it (the script) it doesn't work.
Last edited by buntolo (2012-01-05 23:44:52)
karol wrote:
xterm -hold -e "cd ~/test/ && ls"
works too.
I don't know what $command do you mean, but this will list th ~/test directory.
It works also for me.
With urxvt and xfce4-terminal don't work.
But xterm is too minimalist, is there an advanced terminal based on it?
And I don't unerstand why it doesn't work for xfce4-terminal but for it does for xterm...
man xfce4-terminal:
-H, --hold
Causes the terminal to be kept around after the child command has terminated
-e, --command=command
Execute command inside the terminal
man xterm:
-hold
Turn on the hold resource, i.e., xterm will not immediately destroy its window when the shell command completes. It will wait until you use the window manager to destroy/kill the window, or if you use the menu entries that send a signal, e.g., HUP or KILL.
-e program [ arguments ... ]
This option specifies the program (and its command line arguments) to be run in the xterm window. It also sets the window title and icon name to be the basename of the program being executed if neither -T nor -n are given on the command line. This must be the last option on the command line.
What the ...?
Is only my opinion that there aren't differences between:
terminal -H -e "cd $dir && $command"
and
xterm -hold -e "cd $dir && $command"
Last edited by buntolo (2012-01-06 13:11:58)
Similar Messages
-
trying to turn off spotlight because it's a resource hog. open terminal and enter command but it won't respond or even ask for password.
Step #1:
In System Preferences click on Spotlight.
Step #2:
Click on the Privacy tab here.
Step #3:
Click on the + button here and when the window opens click on "Macintosh HD" (or whatever you've named your HD) and then click the Choose button. -
Open Terminal and Run Commands
Hi I have to ping several IP adresses on a regular basis. Therefore I would like to create an Applescript application that will run terminal and ping the specified IPs so that I can see the results. Any ideas on how to best complete this?
This script will prompt you to enter the number of times you want to ping each server. You'll need to specify the servers to ping in the "set serverAddresses..." line. It writes the results of each ping to a text file on your desktop and opens the file when finished. The next time you run the script if the text file exists it deletes it first.
set theDesktop to (path to desktop)
set serverAddresses to {"www.microsoft.com", "www.apple.com", "www.cnet.com"}
set pingTimes to text returned of (display dialog "Ping each server how many times?" default answer "1" giving up after 10)
tell application "Finder"
if exists file (theDesktop & "ping replies.txt" as string) then
delete file (theDesktop & "ping replies.txt" as string)
end if
set fileID to (open for access file (theDesktop & "ping replies.txt" as string) with write permission)
repeat with aServer in serverAddresses
try
set pingReply to (do shell script "ping -c " & pingTimes & " " & aServer)
on error
set pingReply to "No reply from server " & aServer
end try
write (pingReply & return & return & "########################" & return & return) to fileID
end repeat
close access fileID
open file (theDesktop & "ping replies.txt" as string)
end tell
Robert -
[solved] script/keybinding to launch terminal and run command
the answer is probably simple. but here's the idea.
i use cmus as my music player. at the moment, i have to open a terminal and then type 'cmus'. i'd like to set up a keybinding to run cmus. (incidentally, i'm trying to do this in i3.)
the code
exec cmus
doesn't work, presumably because 'cmus' has to be loaded from within the terminal. (is that right?) what's the syntax for passing on the command 'cmus' to the terminal? what's the best way to do this?
Last edited by anti-destin (2012-03-15 23:52:40)bindsym mod+c exec urxvtc -name cmus -e cmus
urxvtc => Your terminal here if you use something else. Most support -name and -e. Please read their man pages.
-name => Useful for xprop. It assigns the WM_CLASS which can be used for tagging clients.
man urxvt
-e command [arguments]
Run the command with its command-line arguments in the urxvt
window; also sets the window title and icon name to be the basename
of the program being executed if neither -title (-T) nor -n are
given on the command line. If this option is used, it must be the
last on the command-line. If there is no -e option then the default
is to run the program specified by the SHELL environment variable
or, failing that, sh(1).
Last edited by Earnestly (2012-03-15 19:19:25) -
My first bash script - update mirrors and then update your system.
Like the title says, this is my first attempt at a bash script.
I wrote it to make it a tiny bit easier for my wife to keep her system up to date without needing to remember the terminal commands. She runs it via an entry in her openbox menu.
It calls on reflector to check all available mirrors for the most up to date and then use rankmirrors to find the fastest 5 and write them to /etc/pacmand.d/mirrorlist
Finally, it syncs and upgrades via the command pacman -Syyu
#!/bin/bash
# runs reflector to optimize mirrors
echo "I'm afraid I can't do that Dave"
sudo reflector -l 5 -r -o /etc/pacman.d/mirrorlist
echo " I hope you trust me..."
echo " "
sleep 2s
# updates system
sudo pacman -SyyuI've scavenged some code to take care of the root thing, but why rebase vs pacman? After reading up on rebase a bit, I'm not quite sure how to use it to complete the upgrade once it's updated everything.
-
Bash script to terminated upon logout,
Hi all,
I have created a bash script - which start recording a camera device for security purpose, but I need the script to finish when the security user is logging out.
Any idea on how to do that?sweetthdevil wrote:
Hi again,
sorry it took some time but I haven't had the time to look yet,
Back to the topic, I added the "pkill mencoder" at the begining of the file but it doesn't seem to work (because when shutting down the pc, the avi file created is broken - the index)
Any suggestion?
Difficult to say like this. If you kill mencoder by hand (not with the logout script); does this work properly? Maybe you have to wait that men,coder actualy exit before continuing:
PID=$(grep mencoder)
kill $PID
wait $PID
(work only if there is a single mencoder process).
But how did you start mencoder? (o processes receive a HUP signal if the terminal from which it has been opened is closed).
Last edited by olive (2011-03-04 00:58:30) -
Differences in writting bash-script in Solaris and in RHEL?
I wrote a script 'checkinstall' as follow and it works fine by RHEL:
[code]
#!/bin/sh
HOSTNAME=hostname
echo $HOSTNAME
if [ $HOSTNAME == "S001AP99-TEST" ]; then
echo This is the wrong machine.\
echo "\nAbouting installation.\n\n"
exit 1
fi
exit 0
[/code]
But when I run this by Solaris I got:
# ./checkinstall
hostname
./checkinstall: test: unknown operator ==
I changed the line HOSTNAME=hostname to HOSTNAME=`hostname` and it outputs the correct hostname.
But I still get error:
# ./checkinstall
S001AP99-TEST
./checkinstall: test: unknown operator ==If you want to create bash scripts, then the first step is to set the right shell in the first line => #!/bin/bash
Then, after this important step, you can try if this works. Oh surprise, it's working. -
Bash script user input and revision technique requested
In a bash script, I have a need to present the contents of a variable to the user, so that he can "revise" the value if needed, or just hit enter if no revision is required. The objective is to reduce keyboard effort to just revising a presented value.
The "hit enter to continue" part is easy.
The "revise" part is the problem for me.
To illustrate using an example:
#! /bin/bash
position_var=30.39
echo -n "Revise Position if required:${position_var}"
#then some kind of read position_var ??
How can I make the position_var available to the user to edit and resubmit?
Thanks
steve.
Last edited by stevepa (2012-02-28 17:31:56)The above solution is good okay for a general shell script, bash has a feature for what you want though. (see below)
If however the content of the variable is bigger (multiple lines, or a really LONG line you don't want to have to retype), you have 2 options:
Write it to a temp file and fire up $EDITOR (if that's not set, you can default to nano or vi, whichever you can find with $(which ...)).
The other idea that comes to mind is using 'expect' to present a prefilled prompt the user can edit... I'll have to test this...
EDIT: Can't think of an easy way to do that with expect... mhh...
EDIT2: Specifically for bash:
read -p "Revise Position if required: " -i "$current" -e new
with the -e flag, $current will be what the line is prefilled with, and $new will contain the result.
Last edited by Blµb (2012-02-28 18:14:55) -
Why does do shell work differntly from running the same bash script in terminal?
i have the following a/s:
set scriptPath to quoted form of ((POSIX path of (path to me)) & lnThumb) set ftn to "" repeat try do shell script scriptPath & " " & ppath & " " & ftn exit repeat on error error_message number error_number if error_number is not -128 then set ftn to (choose file with prompt "select folder thumbnail" default location dir without invisibles) set ftn to quoted form of the POSIX path of ftn end if end try end repeat
calling this bash script:
#!/bin/bash -f cd "$1" rm ./folderthumb.jpg if [ $# -gt 1 ] then ft=${2/#"$1"\//} else ft=$(awk '/folderIcon=/{printf("%s",$1)}' meta.properties | sed s/folderIcon=// ) if [ 0$ft -eq 0 ] then exit 1 fi ft="thumbs/"$ft fi ln -s $ft ./folderthumb.jpg exit 0
which works as expected when run from the cli:
tomw> /Applications/imageProc/fixJAlbum.app/Contents/Resources/Scripts/lnThumb.sh `pwd` `pwd`/thumbs/P1040161.JPG tomw> ls -l folderthumb.jpg lrwxr-xr-x 1 tomw admin 19 Oct 22 10:36 folderthumb.jpg -> thumbs/P1040161.JPG
but fails to strip the common directory when called from do shell:
tomw> ls -l folderthumb.jpg lrwxr-xr-x 1 tomw admin 89 Oct 22 10:36 folderthumb.jpg -> /Users/tomw/Desktop/trip2albequerque/albequerque/oldRailyards2nobHill/thumbs/P1 040133.JPG
w-t-f???Click on the "Use Advanced editor" on the upper Right of the edit box.
Select the text you want preserved
Click on the >> icon then select "Syntax Highlighting" -> "Plain"
And you will get this
kind of
output when you post -
Shell Script - Need to Open terminal and Print output?
Hi All,
I have a script to update a git version of a software, but I would like to be able to check the output of the script in a terminal.
So in a nut shell, I want when launching the script:
- Terminal to Open
- All command to be executed in the terminal
- Terminal to close only if no error (do not know if it's possible?)
See below my very simple script:
Many thanks in advance for your time and advice,
rm -r /home/sweetth/banshee &&
cd /home/sweetth &&
git clone git://git.gnome.org/banshee &&
cd /home/sweetth/banshee &&
./autogen.sh &&
makeRight, I manage it differently
So I got my script to dl those PKGBUILD for those Ipod library needed for the latest Banshee, then dl the latest banshee git and "make" it.
for those who might be interested to keep their banshee-git update:
#!/bin/bash
xterm -hold -e "mkdir ~/libpod;
cd ~/libpod;
mkdir ~/libpod/gtk-sharp-beans-git
cd ~/libpod/gtk-sharp-beans-git
wget http://aur.archlinux.org/packages/gtk-sharp-beans-git/gtk-sharp-beans-git/PKGBUILD;
wget http://aur.archlinux.org/packages/gtk-sharp-beans-git/gtk-sharp-beans-git/autogen_fix.patch;
makepkg -s;
mv gtk-sharp-beans-git** ~/libpod;
cd ~/libpod;
rm -rf ~/libpod/gtk-sharp-beans-git;
mkdir ~/libpod/gio-sharp-git;
cd ~/libpod/gio-sharp-git;
wget http://aur.archlinux.org/packages/gio-sharp-git/gio-sharp-git/PKGBUILD;
makepkg -s;
mv gio-sharp-git** ~/libpod;
cd ~/libpod;
rm -rf ~/libpod/gio-sharp-git;
mkdir ~/libpod/gkeyfile-sharp-git;
cd ~/libpod/gkeyfile-sharp-git;
wget http://aur.archlinux.org/packages/gkeyfile-sharp-git/gkeyfile-sharp-git/PKGBUILD;
makepkg -s;
mv gkeyfile-sharp-git** ~/libpod;
cd ~/libpod;
rm -rf ~/libpod/gkeyfile-sharp-git;
mkdir ~/libpod/gudev-sharp-git;
cd ~/libpod/gudev-sharp-git;
wget http://aur.archlinux.org/packages/gudev-sharp-git/gudev-sharp-git/PKGBUILD;
makepkg -s;
mv gudev-sharp-git** ~/libpod;
cd ~/libpod;
rm -rf ~/libpod/gudev-sharp-git;
mkdir ~/libpod/libpod-sharp-git;
cd ~/libpod/libpod-sharp-git;
wget http://aur.archlinux.org/packages/libgpod-sharp-git/libgpod-sharp-git/PKGBUILD;
makepkg -s;
mv libgpod-sharp-git** ~/libpod;
rm -rf ~/libpod/libpod-sharp-git;
cd ~/libpod;
sudo pacman -U gtk-sharp-beans-git** gio-sharp-git** gkeyfile-sharp-git** gudev-sharp-git** libgpod-sharp-git**;
rm -rf ~/banshee;
cd ~;
git clone git://git.gnome.org/banshee;
cd ~/banshee;
./autogen.sh;
make;
rm bin/Banshee.NotificationArea.dll*" -
Bash scripting - Opening up new terminals in precise places.
Hi! (I'm a begginer with Linux)
My aim is to write a script (probably in bash - easiest?), which would open up my own "Application Design Enviroment" meaning opening up a terminal in top right corner of a given width with given commands at the beggining, Eclipse on the left side, Web Browser on right bottom etc...
I'd be happy if you help me a bit... What should I start with? Are there any concrete libraries that are responsible for starting apps in precise places on the screen? With given size? Commands?Something like this is going to be specific to your window manager or desktop environment. Luckily, several of them have this feature available if you know how to configure it. What WM/DE are you using?
for example, if you are using Fluxbox, you would simply add an entry to your ~.fluxbox/apps file.
Last edited by Cyrusm (2010-11-25 13:57:28) -
Bash script read input and feh
I was trying to make a quick script to organize some pictures using feh to display the picture in question and then using bash to read a character from the keyboard and move to a folder based on that letter. Problem is it opens every image in the folder at the start (which is overwhelming). I want to go image by image.
heres the simplified script. There would be different if statements for each letter/dir pair
#!/bin/bash
ls | while read file1; do #while loop over all images in the dir
feh $file1 &
read ch1
if [[ $ch1 == "e" ]]
then
mv $file1 /PATH/hawaii
echo "moving to hawaii"
pkill feh
fi
Problem seems to be that the script doesn't wait to receive the input $ch1 before moving on to the next iteration of the loop. Except that when I do something simpler like the following it does seem to wait on the input
while
echo '1'
read ch1
echo '2'
echo '3'for f in *; do $(feh $f)& read g; if [[ "$g" == "e" ]]; then mv -v $f ../tested; fi;pkill feh; done;
waits for me to enter something before checking whether to move the image and closing the image and moving to the next. -
Bash scripting, check filetype with file command
Hi community,
I've got as homework to write a script which uses the command file to check if a file is really a text file, and if yes echo its name.
I wrote the following
#!/bin/bash
for filetype in *
do
a= file $filetype | cut -f2 -d' '
b="ASCII"
if [ "$a"="$b" ]
then
echo "$filetype"
fi
done
however, this does not work.
It just echoes the filetype and all the filenames.
directory
Desktop
Bourne-Again
existe
gzip
kernel26-one-2.6.27-3-i686.pkg.tar.gz
Bourne-Again
list
Bourne-Again
pidof
empty
ps
Bourne-Again
realjpg
Bourne-Again
taille
ASCII
temp.txt
directory
themes
Any ideas?
Why is the filetype echoed, when I declare it as a variable?
Why does the condition thing not work?
Basically, what am I missing?thanks for the ressource.
here's the link
http://tldp.org/LDP/abs/html/
Thanks also for the answers, they were really helpful.
ok, I wanted to post another problem I had but while I was pasting my code in here I realised my mistake. -
[SOLVED] Opening Shell and runnning command(s)
Hi,
I have just discovered the wonderful program xbindkeys, and am thus setting all my keybindings. I ran into a problem when I came to adding the music keybinding [EDIT]. I don't use a gui, just command line mplayer, so this is a bit problematic, as I want to be able to launch a shell and then run a command inside that shell (in this case cd /home/jhl/music/mplaylist && mplayer *). So when I try :
konsole -e cd /home/jhl/music/myplaylist && mplayer *
It does not do what I expected . I've looked around here and on google for an answer, but have yet to find one.
All help is greatly appreciated .
-Josh
Last edited by luskjh (2009-07-12 17:00:12)Crouse's method worked (thanks crouse )
And the [EDIT] was because I repeated 'adding the music keybindings' twice, and didn't catch it when reviewing the thread, opps .
Thanks for all the help .
-Josh -
[solved] rox run action - open xterm and run command?
Probably a really simple question but I'm not having any luck.
I want to set a run action in rox for mp3 files to bring up xterm and run mpg123 in it. I've tried pipes and redirects but they don't work. Any ideas?You're welcome
Maybe you are looking for
-
I just got it back from the apple store and they reset it (becuase it kept shutting down and not turning back on). I put in a new password because i needed to start over from factory settings, but now it doesnt let me log in anymore. (I don't know if
-
Why do I have to pay for free aps?
Why do I have to pay for free ads. I look in my bank Statements and says they charge like 3 dollars but the ad Says free. This isnt right nor is it fair I already paid 300 For the itouch why charge me for the free aps. It's not like I Don't buy thing
-
Dynamically assigning document type variables to RenderPDFForm service
I have a process that takes an xml input, parses it and gets the value from one of its tags. Later I want to feed this value to the next activity in the process that has RenderPDFForm service. The name of the 'Form' input to RenderPDFForm should be t
-
I have a problem here guys, I will deploy cisco NAC with wireless users My scenario is IB-VG , the access points are autonomous there is no WLC the AP is connected to the switch on a trunk port and I have configured the AP with different SSIDs each o
-
Hi, I've been convinced to try out CS5 and add Quite Imposing as the page imposition software to replace InBooklet, even though its price is quite imposing. So, I tried opening one little test CS2 indd document with Illustrator 10 drawings and ran in