ubuntu下寫shell文件實現在多個終端下分別執行命令


#!/bin/bash
 
source ~/catkin_navigation/devel/setup.bash
{
	gnome-terminal -t "XXD_ros" -x bash -c "roscore;exec bash"
}&
 
sleep 1s
{
	gnome-terminal -t "XXD_ms" -x bash -c "rosrun loitor_stereo_visensor loitor_stereo_visensor /home/zjd/catkin_ws1/src/loitor_stereo_visensor/Loitor_VISensor_Setups.txt;exec bash"
}&
 
sleep 1s
{
	gnome-terminal -t "XXD_cam0" -x bash -c "rosrun image_view image_view image:=/cam0/image_raw;exec bash"
}&
 
sleep 1s
{
	gnome-terminal -t "XXD_cam1" -x bash -c "rosrun image_view image_view image:=/cam1/image_raw;exec bash"
}&
 
sleep 1s
{
	gnome-terminal -t "XXD_imu" -x bash -c "rostopic echo /imu0;exec bash"
}&
 
sleep 10s
{
	gnome-terminal -t "XXD_record" -x bash -c "cd;rosbag record /cam0/image_raw /cam1/image_raw /imu0;exec bash"
} 

參考:(http://blog.csdn.net/swust_long/article/details/7285147) [基本用法]

gnome-terminal命令用於打開一個新的終端,直接在命令行
$ gnome-terminal
就可以打開一個新的終端,有一些常用參數:
打開後自動最大化
$ gnome-terminal --maximize
打開後全屏
$ gnome-terminal --full-screen
設置標題
$ gnome-terminal --title=“new title”
打開多個終端,多個tab
$ gnome-terminal --window --window #打開兩個
$ gnome-terminal --window --tab --window --tab --tab #打開兩個,第一個兩個tab,第二個3個tab
設置打開的位置和大小(寬度x高度+左側偏移量+上方偏移量)
$ gnome-terminal --geometry=80x25+10+10
[啓動後自動執行命令]
有兩個參數可以實現這個功能,-e和-x,這兩個區別在於:
-e 可以出現多次,如果在所有–window前面,表示對所有window和tab起作用,
如果在–window或者–tab後面,表示只針對這個tab執行,要注意-e後面只能有一個參數
也就是說如果有空格,需要用引號,具體見後例
-x 只能出現一次,在-x後面的所有內容,均認爲是要執行的命令,所以可以出現空格
這些命令是針對所有tab都執行的

比如:
$ gnome-terminal -e ls
$ gnome-terminal -x ls
這兩個的執行結果都一樣,就是新的終端閃一下就沒了,有幾種辦法:
一種是修改terminal的配置,在terminal點右鍵,選擇Profiles->Profile Preferences
然後找到Title and Command,裏面有一項When command exits,後面選擇爲
Hold the terminal open,然後就可以了
第二種是把結果重定向給less,這樣less執行完之前,是不會退出的
$ gnome-terminal -x ls|less
第三種是在bash裏面再啓用一個bash
$ gnome-terminal -x bash -c “ls; exec bash”
$ gnome-terminal -e ‘bash -c “ls; exec bash”’
注意最後一個命令是exec bash,如果直接寫bash也行,相當於開了一個子shell,這樣有個
缺點,就是直接按關閉按鈕的話,會提示還有程序在運行
需要注意的是,這裏執行的命令,在調用.bashrc之前,所有.bashrc的所有配置都無效
如果需要用到.bashrc裏面的內容,有個變通的辦法,在.bashrc最後加上一句

其他內容
#最後加上這句

evel "$RUN_AFTER_BASHRC" 

然後在寫命令的時候,修改RUN_AFTER_BASHRC這個變量,就可以實現讓.bashrc調用這個命令
$ gnome-terminal -x bash -c ‘export RUN_AFTER_BASHRC=“ls --help”; exec bash’
或者下面的寫法比較簡單(注意對所有tab生效),也不會自動關閉了
$ RUN_AFTER_BASHRC=“ls” gnome-terminal
來源:http://superuser.com/questions/198015/open-gnome-terminal-programmatically-and-execute-commands-after-bashrc-was-execu
[打開一個terminal並預先輸好一段文字]
來源:http://askubuntu.com/questions/5363/how-to-start-a-terminal-with-certain-text-already-input-on-the-command-line
這個需求比較偏門,解決方法不錯,就是利用expect這個工具,最後的程序如下

    #!/usr/bin/expect
    #trap sigwinch and pass it to the child we spawned
    #this allows the gnome-terminal window to be resized
    trap {
      set rows [stty rows]
        set cols [stty columns]
        stty rows $rows columns $cols < $spawn_out(slave,name)
    } WINCH
    set arg1 [lindex $argv 0]
    # Get a Bash shell
    spawn -noecho bash
    # Wait for a prompt
    expect "$ "
    # Type something
    send $arg1
    # Hand over control to the user
    interact
    exit 

調用方法(注意文件的路徑要寫全):
gnome-terminal -e “./myprompt “my text to be posted””
[添加一個”用vim打開“的腳本]
研究這個gnome-terminal的目的,是爲了寫一個腳本,實現右鍵選一個文件,然後可以
用vim打開
最後的腳本文件如下(兩種方法都寫了)

#!/bin/bash
gnome-terminal -x bash -c 'export RUN_AFTER_BASHRC="vim $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"; exec bash'
#RUN_AFTER_BASHRC="vim $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" gnome-terminal

不過選中多個文件還不行,因爲也沒這個需要,所以暫時就這樣了
vim: ft=mynotes

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章