Cut a script name to create a blank file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut a script name to create a blank file
# 1  
Old 06-18-2014
Cut a script name to create a blank file

I have a requirement to read a list of script names create a .finish blank file for each of them.
The script names can be like:
- ScriptName.sh
- ScriptName.sh Param1
- ScriptName.sh Param1 Param2

I need to create files like below using the same script:
- ScriptName.finished
- ScriptName.Param1.finished
- ScriptName.Param1.Param2.finished


Eg ListFile.txt:
Code:
test1.sh
test2.sh PC1
test3.sh PC2 PC21
test4.sh 
test5.sh PC3
test6.sh PC4 PC41

I am trying to read these script names in a loop and cut the 3 values to create it.

Code:
while read SCRIPT_NAME
do
   scr=`echo $SCRIPT_NAME | cut -d "." -f1`
   par1=`echo $SCRIPT_NAME | cut -d " " -f2`
   par2=`echo $SCRIPT_NAME | cut -d " " -f3`

  ##Condition here to check if par1 and par2 are blank and then create a blank file
done <"ListFile.txt"

In first case test1.sh, all three values are given as test1, test1.sh, test1.sh
Since cut has nothing after first space, should it not have the value for par1 and par2 as blank?

I am using ksh. Is there an easier way to do this?
Thanks in advance for the help.

Last edited by Don Cragun; 06-18-2014 at 05:25 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 06-18-2014
These are much easier to do with variable substitution. It sounds hard to get started, but it's easier when you get the hang of it.

Is SCRIPT_NAME a value you will read as input or do you want it to be the name of the script you are already running? The current running script will be known as $0, parm 1 is $1, parm 2 is $2 etc.

If you are using this, then you can slice the values up however you like, such as:-
Code:
SCRIPT_NAME=$0
echo $SCRIPT_NAME

SCRIPT_NAME="${0##*/}"               # Trim off everything up to and including the last /, i.e. the path.
echo $SCRIPT_NAME

SCRIPT_NAME="${SCRIPT_NAME%.sh}"     # Trim off a trailing .sh from what you've already got
echo $SCRIPT_NAME

PARM1=$1
echo $PARM1

You might be wanting to read from an input file as you seem to suggest with your done <"ListFile.txt" so you could also do this:-
Code:
while read scr par1 par2
do
   trimmed="${scr%.sh}"
   echo "I'm running $trimmed with parm1=$par1 and parm2=$par2"
   echo "My completion flag will be $trimmed.$par1.$par2.finished"
done < listfile.txt

Does that help?

Be aware that if you have three parameters, then this while read will put the 2nd & 3rd parameter into $par2


Robin

Last edited by rbatte1; 06-18-2014 at 12:51 PM.. Reason: Added parm3 warning
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 06-18-2014
This is really helpful. Thanks much Robin. Reading the parametes in while loop makes it so much easier.

I will need to check that the par1 and par2 are not blank and create the .finished file.
If both of them are blank, then the file will be say $trimmed.finished, if par1 is present then $trimmed.$par1.finished, if both are present $trimmed.$par1.$par2.finished.

Is an if condition the best way? Or Is there an easier way to check if a variable is null and use it if not don't while appending?
# 4  
Old 06-18-2014
Standard POSIX parameter expansions do everything you seem to want here:
Code:
#!/bin/ksh
while read base e1 e2
do	touch "${base%.*}${e1:+.$e1}${e2:+.$e2}.finished"
done < ListFile.txt

${base%.*} will remove the last period and everything following it from the 1st word on a line in the input file. ${e1:+.$e1} expands to an empty string if the 2nd word on a line from the input file was not present or to a period followed by that word if it was present. ${e2:+.$e2} does the same thing for the 3rd word.

With the ListFile.txt from the first message in this thread, this script will create (or update the timestamps to the current time if they already exist) the files:
Code:
test1.finished
test2.PC1.finished
test3.PC2.PC21.finished
test4.finished
test5.PC3.finished
test6.PC4.PC41.finished

These 2 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 06-19-2014
Don_Cragun,

Well, that's a really great trick. Just to clarify, what I think you've done is build up the filename with these rules:-
  • If $e1 is null, then that part is null
  • If $e1 is not null, then that part is ".$e1"
It's a lovely bit of logic removing a mass of if statements or the overkill of case.


Many thanks, Smilie
Robin
# 6  
Old 06-19-2014
Using awk

Code:
[akshay@nio tmp]$ cat file
test1.sh
test2.sh PC1
test3.sh PC2 PC21
test4.sh 
test5.sh PC3
test6.sh PC4 PC41

[akshay@nio tmp]$ awk '{sub(/\..*/,x,$1); $1=$1; $NF=$NF".finished"; $0 = "touch "$0}1' OFS="."  file | sh

[akshay@nio tmp]$ ls *.finished -1
test1.finished
test2.PC1.finished
test3.PC2.PC21.finished
test4.finished
test5.PC3.finished
test6.PC4.PC41.finished

This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 06-23-2014
This is a really great way of doing what I am trying to acomplish. And definately eleminates unwanted if statement. Its a great new learn for me.
This User Gave Thanks to member2014 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using :<<cut / cut to comment out block of bash script

I am using : << cut / cut to comment out block of code. Works fine on few lines of script, then it gives me this cryptic error when I try to comment out about 80 lines. The "warning " is at last line of script. done < results 169 echo "END read all positioning parameters" 170... (8 Replies)
Discussion started by: annacreek
8 Replies

2. Shell Programming and Scripting

Script to find blank records in a file except for few columns

I have a file with the following format: X|High|2|GIC|DM||XHM|||6 Months X|Moderate|2|GIC|DM||XHM|||6 Months X|High|2|GCM|DM||XSF|||6 Months X|Med|2|GCM|DM||XSF|||6 Here there are ten columns but I need to print rows having blank records in any of the rows (except for 6th,8th and 9th... (10 Replies)
Discussion started by: chatwithsaurav
10 Replies

3. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : ®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

4. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

5. Shell Programming and Scripting

Remove Space and blank line from file in UNIX shell script

I have below file. I want to remove space at begining of every line and then after also remove blank line from file. I use below code for each operation. sed -e 's/^*//' < check.txt > check1.txt sed '/^\s*$/d' < check1.txt > check2.txt above code not remove all the space... (12 Replies)
Discussion started by: Mohin Jain
12 Replies

6. Shell Programming and Scripting

How to cut data from file and create another file.

I have file which has more than 1000 lines. PFB file info Line 1. <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAPSDK1="http://www.w3.org/2001/XMLSchema" xmlns:SOAPSDK2="http://www.w3.org/2001/XMLSchema-instance"... (8 Replies)
Discussion started by: humaemo
8 Replies

7. Shell Programming and Scripting

Cut last blank space

Hello, I am using this to get only directories : ls -l | grep '^d'and here is the result : drwx------ 13 so_nic sonic 13 Nov 4 13:03 GLARY drwx------ 3 so_nic sonic 3 May 6 2010 PSY2R drwx------ 15 so_nic sonic 15 Oct 14 08:47 PSYR1 But I only need to keep this... (7 Replies)
Discussion started by: Aswex
7 Replies

8. Red Hat

Script to remove a blank in a file on Redhat

Hi, I'm running 2.6.9-55.ELsmp. I'm looking for a script to remove a blank line on the first line of a file. The script must also test if the file is 0k, if yes then do nothing, but if the file is > 0k then check if the first line is not blank, if yes then remove the line then save it. ... (4 Replies)
Discussion started by: lamoul
4 Replies

9. Shell Programming and Scripting

How to delete the blank file while running a script.

Hi All, When i am runing a script from home directory the output is fine, but whenever i am running a script from any of the directory the output is fine while a "Blank file" is created named as "1" in the particular directory. Will you please suggest any options to fix this problem. It... (1 Reply)
Discussion started by: r_jha17
1 Replies

10. Shell Programming and Scripting

Blank Space is not appending in each row of CSV File - Shell Script

I am calling SQL script in my UNIX Shell script and trying to create the CSV file and my last column value of each row is 23 blank spaces. In my SQL script,the last column is like below. RPAD(' ',23,' ') -- Padding 23 blank Spaces The CSV file is generated but the sapce(23 spaces) is... (2 Replies)
Discussion started by: praka
2 Replies
Login or Register to Ask a Question