LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-25-2012, 04:22 AM   #1
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Rep: Reputation: 30
Change name


I have a shell script , the content is below , the script will attach a file /tmp/attach.txt to the mail and send it out .
"
"
CHR=`cat file.txt |head -10 |cut -c 2-7`
"
do
mail -a /tmp/attach.txt email_address
done
"
"

Now , if I would like to add one checking , if the content of $CHR is begins with A ( eg. A12345 ) , then change attach file name ( from attach.txt to A123456.txt ) , so that the user see the attach file name is A123456.txt , if not begins with A , then continue to use the file name attach.txt as the attach file name .

Can advise what can I do ?

thanks.

Last edited by ust; 09-25-2012 at 04:27 AM.
 
Old 09-25-2012, 05:32 AM   #2
Neron.L
LQ Newbie
 
Registered: Sep 2012
Posts: 5

Rep: Reputation: Disabled
Hi, Maybe you can extract $CHR with the length that you want, and add a checking for the extracted string.
 
Old 09-25-2012, 06:07 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Use bash regex
Code:
v1=A12345
if [[ $v1 =~ ^A ]]
then
	echo match
	fname=/tmp/${v1}.txt
	echo $fname
else
	echo nomatch
fi
 
Old 09-25-2012, 10:10 AM   #4
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by chrism01 View Post
Use bash regex
Code:
v1=A12345
if [[ $v1 =~ ^A ]]
then
	echo match
	fname=/tmp/${v1}.txt
	echo $fname
else
	echo nomatch
fi
thanks reply,

I have two questions .

For my requirement , I would like to change the attachment file name from attach.txt to A12345.txt if meet the condition , so that the user see the attachment file name is A12345.txt , how to do it ?

Thanks.

Last edited by ust; 09-25-2012 at 10:34 AM.
 
Old 09-25-2012, 11:02 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
I don't script like that but you probably understand the checks better this way:
Code:
#!/bin/bash
# Debug until satisfied
set -vx
# Defaults
LANG=C; LC_ALL=C; export LANG LC_ALL; 
# Should have a file
if [ -f "/path/to/file.txt" ]; then
 # Strings or 'cat -v', then chop off
 WHATEVER=$(strings -an1 /path/to/file.txt 2>/dev/null|head -10|cut -c 2-7)
 # The variable shouldn't be empty
 if [ ${#WHATEVER} -ne 0 ]; then
  # The variable should not contain trickiness
  if [ "X${WHATEVER//[A-Za-z0-9]/}" = "X" ]; then
   # Give me an A...
   if [ "${WHATEVER:0:1}" = "A" ]; then
    # If you want the attachment to have a specific name then its easiest to move it
    mv /path/to/file.txt "/path/to/${WHATEVER// /}.txt"
    mpack -s "Here is ${WHATEVER// /}.txt" "/path/to/${WHATEVER// /}.txt" re.ci@pie.nt
   else
    mpack -s "Here is file.txt" "/path/to/file.txt" re.ci@pie.nt
   fi
  else
   echo "A char is a char is a char of course."
  fi
 else
  echo "I didn't get that?"
 fi
else
 echo "No such file."
fi
exit 0
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How can I change file permissions in windows ntfs partition.Even root can't change it e3399 Linux - Newbie 3 03-02-2011 11:54 PM
WEP Encryption Key:26chars_what choices must change in ubuntu 9.10 for the new change lse123 Linux - Newbie 1 03-02-2010 03:38 AM
is there a linux 'change' prog (like old dos 1) for quick change of text in files? johnemac General 2 06-15-2009 12:22 PM
Cannot Change GNOME Menu Items to add/change Item for Mozilla 1.2 After Upgrade metoome General 0 03-27-2004 01:19 AM
Cannot Change GNOME Menu Items to add/change Item for Mozilla 1.3 After Upgrade SForsgren Linux - General 4 03-27-2004 12:43 AM


All times are GMT -5. The time now is 05:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration