Refreshing a file in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Refreshing a file in bash
# 1  
Old 01-08-2014
Refreshing a file in bash

I am creating a file when running a bash script as shown. I want to refresh the file every time, however touch only changes the time stamp. I also need the file with execute permission. I have included redirection ">" the first time to solve the problem (i=1).

Any suggestions how I can simplify this better?

Code:
execfl="rmlines#${arg_rexp}.x"

touch $execfl
chmod a+x $execfl

i=1
for f in $arg_files ; do
  echo "titl = $f"
  name="`echo $f | sed -e 's|.[^.]*$||'`"
  extn="`expr $f : '.*\.\([^.]*\)$'`"
  intmd_fl="$f.intmd"
  if [ $extn != "x" ]; then
    echo "$i"
    if (( i == 1 )); then
      echo "grep -v "$rexp" $f > $intmd_fl" > $execfl
    else
      echo "grep -v "$rexp" $f > $intmd_fl" >> $execfl
    fi
    echo "mv $intmd_fl > $f" >> $execfl
    echo "" >> $execfl
  fi
  ((i++))
done

---------- Post updated at 08:01 AM ---------- Previous update was at 07:39 AM ----------

Modified to the following. A bit better now.

Code:
  execfl="rmlines#${arg_rexp}.x"

  i=1
  for f in $arg_files ; do
    name="`echo $f | sed -e 's|.[^.]*$||'`"
    extn="`expr $f : '.*\.\([^.]*\)$'`"
    intmd_fl="$f.intmd"
    if [ $extn != "x" ]; then
      if (( i == 1 )); then
        echo "grep -v "$arg_rexp" $f > $intmd_fl" > $execfl
      else
        echo "grep -v "$arg_rexp" $f > $intmd_fl" >> $execfl
      fi
      echo "mv $intmd_fl $f" >> $execfl
      echo "" >> $execfl
    fi
    ((i++))
  done

# permit execution
  chmod a+x $execfl

# 2  
Old 01-08-2014
Quote:
Originally Posted by kristinu

Code:
    if (( i == 1 )); then
      echo "grep -v "$rexp" $f > $intmd_fl" > $execfl
    else
      echo "grep -v "$rexp" $f > $intmd_fl" >> $execfl
    fi

Here you can just use >> as it create the file as well. No need for a check here.

To get the file name you can use:
Code:
name=${f%.*}

and to get the extension you can use
Code:
extn=${f##*.}


Last edited by chacko193; 01-09-2014 at 06:47 AM.. Reason: Correction
# 3  
Old 01-08-2014
Not if the user executed the script a second time. That's why I used ">" first time
# 4  
Old 01-09-2014
Quote:
Originally Posted by kristinu
Not if the user executed the script a second time. That's why I used ">" first time
If you want to clear the file before the script is executed, why not just do that first?
# 5  
Old 01-09-2014
Will need to clear the file if it exists then. What would you call fir doing this?
# 6  
Old 01-09-2014
Quote:
Originally Posted by kristinu
Will need to clear the file if it exists then. What would you call fir doing this?
To check if the file exists and then clear it use
Code:
[ -f $execfl ] && > $execfl

And why would you want check if the file exists in the first place? Anyway you are creating the file. So you can just do
Code:
 > $execfl

before the loop insted of including a check for the file.

Last edited by chacko193; 01-09-2014 at 06:54 AM.. Reason: better solution
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Call one bash file from another bash file.

hi .. greetings ...i need help in bash programming. i am on linux mint. i have two .sh files.(netcheck.sh and dlinkreboot.sh).. please note . both bash files are working perfectly well independently... but cant work together. . in short i want one bash file to call the other bash file... (2 Replies)
Discussion started by: emmfranklin
2 Replies

2. Shell Programming and Scripting

Find matching file in bash with variable file names but consisent prefixs

As part of a bash the below line strips off a numerical prefix from directory 1 to search for in directory 2. for file in /home/cmccabe/Desktop/comparison/missing/*.txt do file1=${file##*/} # Strip off directory getprefix=${file1%%_*.txt} ... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Bash to select text and apply it to a selected file in bash

In the bash below I am asking the user for a panel and reading that into bed. Then asking the user for a file and reading that into file1.Is the grep in bold the correct way to apply the selected panel to the file? I am getting a syntax error. Thank you :) ... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Shell Programming and Scripting

Problem in refreshing a text editor (gedit) for scanned pdf

Dear Friends, I am using Ubuntu 15.10, 34 bit system. I added a Nautilus-Actions script in shell script to convert PDF files to text. There are 2 types of PDF 1. Scanned PDF -- Not OCR type -- When I convert it to text it work , but as the part it must (text file) open in gedit . But I can see... (2 Replies)
Discussion started by: anespa
2 Replies

5. Shell Programming and Scripting

Bash to search file based off user input then create new file

In the below bash a file is downloaded when the program is opened and then that file is searched based on user input and the result is written to a new file. For example, the bash is opened and the download.txt is downloaded, the user then enters the id (NA04520). The id is used to search... (5 Replies)
Discussion started by: cmccabe
5 Replies

6. UNIX for Dummies Questions & Answers

PS1 directory path not refreshing...

Hi, I am using Unix, I have added below in the .profile to change my prompt value. PS1=`logname`@`hostname`:$PWD# The problem is, after changing the directory, path is not being refreshed. Is there anything wrong? Thanks, Milind (2 Replies)
Discussion started by: mgpatil31
2 Replies

7. Post Here to Contact Site Administrators and Moderators

refreshing page

Dear Admins, I think it would be good if the page is auto-refreshed after some duration. generally I do a manual reload/refresh from browser to get the new post/updates. I am not very sure if its already being done (may be the duration is too long?). (1 Reply)
Discussion started by: clx
1 Replies

8. Shell Programming and Scripting

[Bash]Attempting to Merge text from one file into another file at the line directly under a word

Hello, This is my first post on the forums. So I want to start by thanking anyone who is kind enough to read this post and offer advise. I hope to be an active contributor now that I've found these forums. I have an issue that I figure would be a good first post.. I have 2 text files... (5 Replies)
Discussion started by: efciem
5 Replies

9. Solaris

weird problem - terminal not refreshing; sleep not terminating

Hi, I am having a wierd problem in one of the Solaris server. root@ussd # uname -a SunOS ussd 5.8 Generic_108528-24 sun4u sparc SUNW,Sun-Fire-280R The problem is that terminal is not refreshing when we run commands like prstat, tail -f "some log file", sar -u 1 10 Also, sleep... (3 Replies)
Discussion started by: vikas027
3 Replies

10. Shell Programming and Scripting

refreshing inetd

Hi I have a question, what is the purpose of this command and what will it do "refresh -s inetd" Thanks in Advance Swaraj (3 Replies)
Discussion started by: kswaraj
3 Replies
Login or Register to Ask a Question