请教高手 小弟刚开始学UNIX。想做个能有下列功能的.sh。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请教高手 小弟刚开始学UNIX。想做个能有下列功能的.sh。相关的知识,希望对你有一定的参考价值。

请教高手
小弟刚开始学UNIX。想做个能有下列功能的.sh。

在bash环境下,想做个sh。功能是把元csv文件的内容 例一行:aa,bb,cc,dd,ee,ff 根据一个外接文件的指定的数字输出顺序如:3 4 2 5 6 1(只是代表元文件内的字母的新的位置), 自动把元csv文件内的字母 改成:cc,dd,bb,ee,ff,aa 这个样子输出显示。用来位置指定的外接文件,可以随便更换,根据新文件的位置指定,数字的位置相应改变。
请哪位高手指点一下,实在是想了很长时间还是无解。

在下不才 也写了个

3个文件
reference.txt 内容为 3 4 2 5 6 1
source.txt 内容为 aa,bb,cc,dd,ee,ff
trans.sh 内容为:

#/bin/bash
sourcefile=$1
referencefile=$2

i=0

char=($(more $referencefile))

while read line
do

while [ $char[$i] ]
do
num=$char[$i]
pos="$"$num

tmpchar=$(echo $line | awk -F , 'print '$pos'')
echo -e "$tmpchar\c" >> tmpfile
i=$(expr $i + 1)
if [ $i -ne $#char[@] ]; then
echo -e ",\c" >> tmpfile
else
echo -e "\t" >> tmpfile
fi

done
done < $sourcefile

调用方式为:
sh trans.sh source.txt reference.txt

产生tmpfile文件,内容为
cc,dd,bb,ee,ff,aa

参考资料:http://hi.baidu.com/loveyurui/blog/item/72bdc9fe36f5d0f3fc037f9f.html

参考技术A 根据你的需求,我也是现学现用shell文件place.sh如下:

#!/bin/ksh
tempifs=$IFS
IFS=,

i=0
while read line1
do
j=0
arr1=($line1)
let i=i+1
while read line2
do
arr2=($line2)
let j=j+1
if [ $j -eq $i ]
then
break
fi
done < ./data/place2.dat
if [ $#arr1[@] -ne $#arr2[@] ]
then
echo "line[$i]: element num not match!!"
IFS=$tempifs
exit
fi
index=0
for var in $arr2[@]
do
let var=var-1
echo -e "$arr1[$var]\c" >> ./data/place3.dat
let index=index+1
if [ $index -ne $#arr1[@] ]
then
echo -e ",\c" >> ./data/place3.dat
fi
done
echo "" >> ./data/place3.dat

done < ./data/place1.dat

IFS=$tempifs

输入文件1--“./data/place1.dat”
aa,bb,cc,dd,ee,ff
aa,bb,cc,dd,ee,ff
输入文件2--“./data/place2.dat”
3,4,2,5,6,1
1,4,2,5,6,3
输出文件3--“./data/place3.dat”
cc,dd,bb,ee,ff,aa
aa,dd,bb,ee,ff,cc

脚本思想是:
循环读两个文件的相同的行,以,为分隔符读入两个数组,再以第2个数组中的元素值-1为第1个数组的下标,重新输出到第3个文件

JAVA 闹钟程序

请高手帮我个忙啊。。我想做个JAVA闹钟。请高手给我个实例代码。。谢谢了。。在网上百度了几个都没用。。(或者我不知道用)。。请高手帮帮忙啊。。小弟我刚学。。谢谢

import java.util.*;
import java.awt.*;
import java.applet.*;
import java.text.*;
import java.awt.event.*;

public class Alarm extends Applet implements Runnable

Thread timer=null; //创建线程timer
Image gif1; //clockp:闹钟的外壳,闹铃和报时物
boolean setflag=false,stopflag=false,cancelflag=false;
Panel setpanel;
//获取声音文件
AudioClip ring=getAudioClip(getCodeBase(), "1.mid");
Button setbutton=new Button("SET");
Button cancelbutton=new Button("CANCEL");
Button stopbutton=new Button("STOP");
//响应按钮事件
private ActionListener setli=new ActionListener()

public void actionPerformed(ActionEvent e)

setflag=true;

;
private ActionListener cancelli=new ActionListener()

public void actionPerformed(ActionEvent e)

setflag=true;

;
private ActionListener stopli=new ActionListener()

public void actionPerformed(ActionEvent e)

ring.stop();
//清除的方法
//g.clearRect(83,280,20,30);

;

Label note1=new Label("Alarm clock:");
//GregorianCalendar提供的是一个日历式的东东,上面又多了很多的参数,是方便操作了不少。而Date类的功能远不及其,求个和日期有联系的还要自己计算。
GregorianCalendar cal=new GregorianCalendar();
GregorianCalendar cal2=new GregorianCalendar();
SimpleDateFormat df=new SimpleDateFormat("yyyy MM dd HH:mm:ss");//设置时间格式
Date dummy=new Date(); //生成Data对象
String lastdate=df.format(dummy);
Font F=new Font("TimesRoman",Font.PLAIN,14);//设置字体格式
Date dat=null;
Date timeNow;
Color fgcol=Color.blue;
Color fgcol2=Color.darkGray;
Color backcolor=Color.blue;
Label hlabel2,mlabel2,slabel2;//显示时间单位时所用的标签(时、分、秒)
int i;
int s,m,h;
TextField sethour,setmin,setsec;//显示当前时间文本框和定时文本框

//在Applet程序中,首先自动调用初始化完成必要的初始化工作,紧接着自动调用start,在进入执行程序和返回到该页面时被调用,而从该页面转到别的页面时,stop被调用,关闭浏览器时,执行destroy。
public void init()//初始化方法

int fieldx=50,fieldy1=120,fieldy2=220,fieldw=30,fieldh=20,space=50;//显示时间和定时文本框的定位参数
setLayout(null); //将布局管理器初始化为null
setpanel=new Panel();
setpanel.setLayout(null);
setpanel.add(note1);
note1.setBounds(30,100,60,20);
note1.setBackground(backcolor);
note1.setForeground(Color.black);
//定时用的文本框(时、分、秒)
sethour=new TextField("00",5);
setmin=new TextField("00",5);
setsec=new TextField("00",5);
hlabel2=new Label();
mlabel2=new Label();
slabel2=new Label();
//定时的小时文本框的位置、大小
setpanel.add(sethour);
sethour.setBounds(fieldx,fieldy2,fieldw,fieldh);
sethour.setBackground(Color.white);
//在文本框后加入单位“时”
setpanel.add(hlabel2);
hlabel2.setText("h");
hlabel2.setBackground(backcolor);
hlabel2.setForeground(Color.black);
hlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);
fieldx=fieldx+space;
//定时的分钟文本框的位置、大小
setpanel.add(setmin);
setmin.setBounds(fieldx,fieldy2,fieldw,fieldh);
setmin.setBackground(Color.white);
//在文本框后加入单位“分”
setpanel.add(mlabel2);
mlabel2.setText("m");
mlabel2.setBackground(backcolor);
mlabel2.setForeground(Color.black);
mlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);
fieldx=fieldx+space;
//定时的秒文本框的位置、大小
setpanel.add(setsec);
setsec.setBounds(fieldx,fieldy2,fieldw,fieldh);
setsec.setBackground(Color.white);
//在文本框后加入单位“秒”
setpanel.add(slabel2);
slabel2.setText("s");
slabel2.setBackground(backcolor);
slabel2.setForeground(Color.black);
slabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);
//设置闹钟控制按钮(on,off)
setpanel.add(cancelbutton);
setpanel.add(setbutton);
setpanel.add(stopbutton);
cancelbutton.setBounds(90,180,40,20);
setbutton.setBounds(140,180,40,20);
stopbutton.setBounds(522,180,40,20);
setbutton.addActionListener(setli);
cancelbutton.addActionListener(cancelli);
stopbutton.addActionListener(stopli);
stopbutton.setVisible(false);
//将面板加入当前容器中,并设置面板的大小和背景色
add(setpanel);
setpanel.setBounds(300,1,250,420);
setpanel.setBackground(backcolor);

/*int xcenter,ycenter,s,m,h;
//闹钟中心点所在位置
xcenter=145;
ycenter=162;
s=(int)cal.get(Calendar.SECOND);
m=(int)cal.get(Calendar.MINUTE);
h=(int)cal.get(Calendar.HOUR_OF_DAY);
//初始化指针位置
lastxs=(int)(Math.cos(s*3.14f/30-3.14f/2)*30+xcenter);
lastys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);
lastxm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);
lastym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);
lastxh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*18+xcenter);
lastyh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*18+ycenter);
lasts=s; */

MediaTracker mt=new MediaTracker(this);//为给定组件创建一个跟踪媒体的MediaTracker对象,把图片添加到被跟踪的图片组

//Java允?Sapplet??HTML所在的位置(decument base)下?d?Y料,也允?Sapplet?钠涑淌酱a所在的位置(code base)下?d?Y料。藉由呼叫getDocumentBase()?cgotCodeBase()可得到URL物件。?@些函?????湍阏业侥阆胂螺d的?n案的位置
//clockp=getImage(getDocumentBase(),"11.png");
gif1=getImage(getCodeBase(),"2.gif");

//i为id号

mt.addImage(gif1,i++);

try

mt.waitForAll();

catch(InterruptedException e)
;//等待加载结束
resize(600,420);//设置窗口大小

//窗口显示有改变的时候调用paint
public void paint(Graphics g)
//重写paint()方法
int xh,yh,xm,ym,xs,ys,strike_times;
int xcenter,ycenter;
String today;
xcenter=148;
ycenter=186;
dat=new Date();
//用当前时间初始化日历时间
cal.setTime(dat);
//读取当前时间
s=(int)cal.get(Calendar.SECOND);
m=(int)cal.get(Calendar.MINUTE);
h=(int)cal.get(Calendar.HOUR_OF_DAY);
//换一种时间表达形式
today=df.format(dat);
//指针位置
xs=(int)(Math.cos(s*3.14f/30-3.14f/2)*30+xcenter);
ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);
xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);
ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);
xh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*12+xcenter);
yh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*12+ycenter);
//设置字体和颜色
g.setFont(F);
//前景色

g.setColor(getBackground()); //取背景色的
g.drawImage(gif1,75,110,this);
//以数字方式显示年、月、日和时间
g.drawString(today,55,415);

//画指针
g.drawLine(xcenter,ycenter,xs,ys);
g.drawLine(xcenter,ycenter-1,xm,ym); //(x1,y1,x2,y2)
g.drawLine(xcenter-1,ycenter,xm,ym);
g.drawLine(xcenter,ycenter-1,xh,yh);
g.drawLine(xcenter-1,ycenter,xh,yh);

int timedelta;//记录当前时间与闹铃定时的时差
Integer currh,currm,currs;//分别记录当前的时、分、秒

Date dat2=new Date();
cal2.setTime(dat2);
//读取当前时间
currh=(int)cal2.get(Calendar.SECOND);
currm=(int)cal2.get(Calendar.MINUTE);
currs=(int)cal2.get(Calendar.HOUR_OF_DAY);
//这样做的话说我API已过时
//timeNow=new Date();
//currh=new Integer(timeNow.getHours());
//currm=new Integer(timeNow.getMinutes());
//currs=new Integer(timeNow.getSeconds());

if(setflag)
//判断是否设置了闹钟
//判断当前时间是否为闹钟所定的时间
if((currh.intValue()==Integer.valueOf(sethour.getText()).intValue())&&(currm.intValue()==Integer.valueOf(setmin.getText()).intValue())&&(currs.intValue()==Integer.valueOf(setsec.getText()).intValue()))

ring.play();
g.drawImage(gif1,83,280,this);
stopbutton.setVisible(true);

timedelta=currm.intValue()*60+currs.intValue()-Integer.valueOf(setmin.getText()).intValue()*60-Integer.valueOf(setsec.getText()).intValue();
if((timedelta>=30))

//若当前时间与闹钟相差时间超过30秒,闹钟自动停
ring.stop();
//清除的方法
g.clearRect(83,280,20,30);


dat=null;


public void start()

if(timer==null)

timer=new Thread(this);//将timer实例化
timer.start();



public void stop()

timer=null;


//给创建线程后start之后自动执行的函数
public void run()

//在run()方法中,调用repaint()方法,以重绘小程序区,进行时钟显示的更新。接着调用sleep方法让当前线程(也就是我们创建的线程clockthread)睡眠1000毫秒,因为我们每秒钟要更新一下显示,所以让它睡眠1秒
while(timer!=null)

try

timer.sleep(1000);

catch(InterruptedException e)

//调用repaint时,会首先清除掉paint方法之前的画的内容,再调用paint方法
repaint();//刷新画面

timer=null;


//当AWT接收到一个applet的重绘请求时,它就调用applet的 update(),默认地,update() 清除applet的背景,然后调用 paint()。重载 update(),将以前在paint()中的绘图代码包含在update()中,从而避免每次重绘时将整个区域清除
//有两种方法可以明显地减弱闪烁:重载 update()或使用双缓冲。
//使用双缓冲技术:另一种减小帧之间闪烁的方法是使用双缓冲,它在许多动画Applet中被使用。其主要原理是创建一个后台图像,将需要绘制的一帧画入图像,然后调用DrawImage()将整个图像一次画到屏幕上去;好处是大部分绘制是离屏的,将离屏图像一次绘至屏幕上比直接在屏幕上绘制要有效得多,大大提高做图的性能。
// 双缓冲可以使动画平滑,但有一个缺点,要分配一张后台图像,如果图像相当大,这将需要很大一块内存;当你使用双缓冲技术时,应重载 update()。

public void update(Graphics g)

Image offscreen_buf=null;
//采用双缓冲技术的update()方法
if(offscreen_buf==null)
offscreen_buf=createImage(600,420);
Graphics offg=offscreen_buf.getGraphics();
offg.clipRect(1,1,599,419);
paint(offg);
Graphics ong=getGraphics();
ong.clipRect(1,1,599,419);
ong.drawImage(offscreen_buf,0,0,this);


/** Creates a new instance of AlarmClock */

参考资料:http://conjs.cn

参考技术A //OK 写好了...怕你不懂 帮你加了注释

package 娱乐;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
public class Alarm extends JFrame implements Runnable
JLabel ri ,shi, fen, miao, dangqian;

JButton queding, dakai;

JTextField music,RI, SHI, FEN, MIAO;

int h=0,f=0,m=0,r=0;
boolean fo=false;
public AudioClip soumd1;
public Alarm()
Container c = getContentPane();
c.setLayout(new GridLayout(3, 1));
JPanel jp = new JPanel();
dangqian = new JLabel();
jp.add(dangqian);
c.add(jp);
JPanel jp1 = new JPanel();
music = new JTextField(20);
dakai = new JButton("选择闹铃音乐");
jp1.add(music);
jp1.add(dakai);
c.add(jp1);
ri = new JLabel("日");
RI = new JTextField(4);
shi = new JLabel("时");
SHI = new JTextField(4);
fen = new JLabel("分");
FEN = new JTextField(4);
miao = new JLabel("秒");
MIAO = new JTextField(4);
JPanel jp2 = new JPanel();
jp2.add(ri);
jp2.add(RI);
jp2.add(shi);
jp2.add(SHI);
jp2.add(fen);
jp2.add(FEN);
jp2.add(miao);
jp2.add(MIAO);
queding = new JButton("确定");
jp2.add(queding);
c.add(jp2);
setSize(400, 130);
setVisible(true);
dakai.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent event)
JFileChooser fileChooser = new JFileChooser(); // 实例化文件选择器
fileChooser
.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 设置文件选择模式,此处为文件和目录均可
fileChooser.setCurrentDirectory(new File(".")); // 设置文件选择器当前目录
fileChooser
.setFileFilter(new javax.swing.filechooser.FileFilter()
public boolean accept(File file) // 可接受的文件类型
String name = file.getName().toLowerCase();
return name.endsWith(".wav")
|| name.endsWith(".au")
|| file.isDirectory();


public String getDescription() // 文件描述
return "音乐文件(*.wav,*.au)";

);
if (fileChooser.showOpenDialog(Alarm.this) == JFileChooser.APPROVE_OPTION) // 弹出文件选择器,并判断是否点击了打开按钮
String fileName = fileChooser.getSelectedFile().getAbsolutePath(); // 得到选择文件或目录的绝对路径
music.setText(fileName);


);
queding.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent event)
if(queding.getText().equals("确定"))
try
r=Integer.parseInt(RI.getText());
h=Integer.parseInt(SHI.getText());
f=Integer.parseInt(FEN.getText());
m=Integer.parseInt(MIAO.getText());
if(1<=h&&h<=31&&0<=h&&h<=23&&0<=f&&f<=59&&0<=m&&m<=59)

fo=true;

else
JOptionPane.showMessageDialog(null, "输入时间错误");

catch(Exception e)
JOptionPane.showMessageDialog(null, "请输入正确的时间");



else

fo=false;
RI.setEditable(true);
SHI.setEditable(true);
FEN.setEditable(true);
MIAO.setEditable(true);
queding.setText("确定");
soumd1.stop();


);



public static void main(String agrs[])
Alarm s = new Alarm();
Thread t1 = new Thread(s);
t1.start();
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


public void run()
while (true)
Date now = new Date();
dangqian.setText("当前时间: " + now.toString());
if(fo)

RI.setEditable(false);
SHI.setEditable(false);
FEN.setEditable(false);
MIAO.setEditable(false);
queding.setText("关闭");
SimpleDateFormat ri=new SimpleDateFormat("dd"); //封装 为了获取日期
SimpleDateFormat shi=new SimpleDateFormat("kk"); //封装 为了获取小时
SimpleDateFormat fen=new SimpleDateFormat("mm"); //封装 为了获取分钟
SimpleDateFormat miao=new SimpleDateFormat("ss"); //封装 为了获取秒钟
int riqi=Integer.parseInt(ri.format(now)); //获取日期
int shizhong=Integer.parseInt(shi.format(now)); //获取小时
int fenzhong=Integer.parseInt(fen.format(now)); //获取分钟
int miaozhong=Integer.parseInt(miao.format(now)); //获取秒钟
if(riqi==r&&shizhong==h&&fenzhong==f&&miaozhong==m) //判断条件

try
soumd1=Applet.newAudioClip(new File(music.getText()).toURL()); //播放音乐
soumd1.loop(); //我设置的是循环播放..这样不起床都不行..
fo=false;
catch (MalformedURLException e)
e.printStackTrace();



try
Thread.sleep(1000);
catch (InterruptedException ie)




本回答被提问者和网友采纳
参考技术B import java.util.Timer;
import java.util.TimerTask;

public class MyDoit
public static void main(String[] args)
new Timer().schedule(new TimerTask()
public void run()
System.out.println("简单的闹钟");

, 0, 1000 * 60 * 60 * 24);

以上是关于请教高手 小弟刚开始学UNIX。想做个能有下列功能的.sh。的主要内容,如果未能解决你的问题,请参考以下文章

WPF高手来帮忙!!界面布局的问题

用java做个下载器难不难?、

Qt的高手们,向你们请教: Qt界面的一个控件,比如说一个GroupBox,如何在其刚开始运行时为不可见,点击按

请教Java SE高手一个简单的“@SuppressWarnings("unlikely-arg-type")”注释的含义是啥?

各位高手,小弟请教关于GetWindowRect,GetClientRect以及ClientToScreen,ScreenToClient的使用!先谢谢啦

winform窗体问题`急等`请高手们帮忙解决