Question on storing a variable from a rename action.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question on storing a variable from a rename action.
# 1  
Old 05-04-2015
Question Question on storing a variable from a rename action.

I have a simple if statement I am working with to validate if a file exist and rename it using the following method:
Code:
if [ -a ".htaccess" ];then mv .htaccess .htaccess.bak_$(date +%F-%T);fi;

The output is just as it should be and I need to have that saved as a variable so that I can implement a revert changes option calling a $var that would equate to the .htaccess.bak_2015-05-04-18:49:13 generated and renaming it back. I realize I could easily make a command that reverts the changes finding the file using regular expressions although that seems dodgy as even if the likelihood of encountering a file named similarly is low it is possible.

My understanding of storing and calling variables is still pretty basic and I gladly welcome any help for a solution and understanding of a save in this manner.

p.s also counts as intro thread. Hello!
# 2  
Old 05-04-2015
simply assign it as a variable first with var= and recall it later as $var. remember, you should almost always quote anything that has a $ in it!
Code:
if [ -a .htaccess ]; then
  htaccess_bak=".htaccess.bak_$(date +%F-%T)"
  mv .htaccess "$htaccess_bak"
fi
...
if [ -n "$htaccess_bak" ]; then # check if this variable is not empty
  mv "$htaccess_bak" .htaccess
fi


Last edited by neutronscott; 05-04-2015 at 08:59 PM.. Reason: add if for revert
This User Gave Thanks to neutronscott For This Post:
# 3  
Old 05-04-2015
Thank you neutronscott, That works perfect!
# 4  
Old 05-05-2015
That works if used in the same shell / script. Once you leave either, the var would be lost, and you need to recreate the file name and better do a -a check again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

2. Shell Programming and Scripting

Storing a variable using sed

I would like to use sed to store a variable. The code is : echo $HEADERREC CUTVAR=$(echo "$HEADERREC"|sed 's/SDV/STR') echo $CUTVAR Output I am getting now: 0012PVGRSCDVSDV 005 00000000000000000000 2014 0.00 sed: Function s/SDV/STR cannot be parsed. Desired... (5 Replies)
Discussion started by: MIA651
5 Replies

3. UNIX for Dummies Questions & Answers

If variable contains a pathname then ACTION item

I have an input file that contains a name of a file followed by the pathname like so input1 /here/is/the/path/to/input1 input2 /here/is/the/path/to/input2 input3 /here/is/the/path/to/input3 I'm trying to write a bash script where if a path name is present, grep for a certain key... (5 Replies)
Discussion started by: tester213
5 Replies

4. Shell Programming and Scripting

Storing output into a variable

My script below seems to be choking because I need the the output of the find command to be stored as a variable that can then be called by used lower in the script. #!/bin/bash cd "/resumes_to_be_completed" var1=find . -mmin -1 -type f \( -name "*.doc" -o -name "*.docx" \)... (1 Reply)
Discussion started by: binary-ninja
1 Replies

5. UNIX for Dummies Questions & Answers

Storing all the PID's in a variable.

Hi, ps -ef|awk '{print $2}' i want to store the result of the above command in a variable. I never worked with arrays in shell scripting. i tried the below code: set a=`ps -ef|awk '{print $2}'` But echo $a returns null. I want to store the content in a variable and retrieve it... (2 Replies)
Discussion started by: pandeesh
2 Replies

6. Shell Programming and Scripting

Storing o/p of a command to a variable

Hi, I have a ftp script there I want to store the o/p of the below command: sftp -b <batch file> user@password cat <batch file> get /remote/file/path/remote_file_name.csv*.gz /local/path Now the problem is that when I fire this command. Then it gives o/p as: File... (7 Replies)
Discussion started by: dips_ag
7 Replies

7. Shell Programming and Scripting

Storing a field within a variable

Hi, I need to figure out a way to to capture the contents of a field that is separated by a pipe sign. Example Data: -100F| some other description -10C| some description | some description As you can see, the length of the field on the left of the pipe can be any length and... (3 Replies)
Discussion started by: doza22
3 Replies

8. Shell Programming and Scripting

About storing the value of wc -l into a variable and then using this value in while

Hi all, I m new to this forum. I ma facing onei issue. I have something like this: length= wc -l < b2| awk '{print $1}' where b2 is filename having detauls like: cat b2 abc1 abc4 xyc3 sbdghf4 but when I do echo "$length" it displays nothing Also I am using awk to overcome... (4 Replies)
Discussion started by: student2009
4 Replies

9. Shell Programming and Scripting

Storing value in a variable

Hi Everyone, I have a code which requires to be stored in different variables and I am achiving it like this. HOST=`echo $RMP | cut -f2 -d:` NAME=`echo $RMP | cut -f3 -d:` DIR=`echo $RMP | cut -f4 -d:` TYPE=`echo $RMP | cut -f5 -d:` Is there any other way of storing value... (2 Replies)
Discussion started by: gehlnar
2 Replies

10. Shell Programming and Scripting

Storing a variable?

I'm writing a bash shell script to backup several mysql databases. This script will run on a daily basis and send a copy to a remote FTP repository. The filenames are in the format DATE.backup.sql. How do I store the DATE variable so I can delete/move/etc the file on the FTP server the next time... (4 Replies)
Discussion started by: hoover90
4 Replies
Login or Register to Ask a Question