Run script

I have a file that the format is as below ,

abc.xxx.yyy.zzz
while yyy is login ID .

And I have a script called script1 is as below

 
if ...
then 
   echo "user id"
else
   ...
fi

Now , I would like to have a script that can do su user to yyy first , similiar as below

if ....
then 
    su - yyy echo "user id"
else
    ....
fi

Can advise what can I do ? thanks

Correct me if I am wrong..

  • You have a file abc.xxx.LOGINID.zzz
  • Script1 that prints "user id"
  • U need script that switch user to "LOGINID" from the file then prints "user id"

I would suggest you to try something like,

if
then
su - `ls abc.xxx* | awk -F"." '{print $3}'` echo "user id"
else
fi

thanks reply ,
the script works .
I have one more question , for the file name abc.xxx.yyy.zzz
If I want to have a command which can remove the last two part of the file name ( in the above file name , the last two part is yyy.zzz ) , that means the file name becomes to aaa.xxx , can advise what can I do ?
thanks.

In that case you will have to make use of renaming function. :wink:

 mv oldname newname 

thanks reply ,

But I will run it from script , not single command , and I may be have more than 1 file.

Thanks.

What's holding your filename: a variable, a file, ...?

if file , what can I do ?

thanks.

Very simplistic approach:

$ awk -F"." '{print $1"."$2}' file
abc.xxx
cde.ddd
kde.fer

sorry , may be I am not explain very clearly

I have a file the file names are as below

111.222.333.444
aaa.bbb.ccc.ddd
mmm.nnn.ooo.ppp

I would like to cut last two part so that the result becomes

111.222
aaa.bbb
mmm.nnn

can advise what can I do ?
thanks

cat file
111.222.333.444
aaa.bbb.ccc.ddd
mmm.nnn.ooo.ppp
awk -F. ' { print $1, $2 } ' OFS=. file
111.222
aaa.bbb
mmm.nnn