Simple unix variables in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple unix variables in script
# 1  
Old 04-27-2010
Simple unix variables in script

I'm creating a script that asks a user for a variable

ex
read filename;
read numberinput;

I also have a bunch of files named file.0 file.1 ... file.55

I'm trying to delete all files (if they exist) about file.$numberinput.

Can someone help me out on how to include the variable as part as the filename?
# 2  
Old 04-27-2010
You can use:

${filename}.${numberinput}
# 3  
Old 04-27-2010
try this

Code:
for i in `seq 1 55`; do [ -s file.$i ] && rm file.$i || echo "file.$i not present";done

# 4  
Old 04-27-2010
I'm trying to figure it out still. I know this isn't the best way to complete the task. Any tips are appreciated.
basically fnom is a log file that I want to rotate in a specific way. when the script is run, the log file will become fnom.1 and every older version is iterated by 1.

After I get the user input for the file location and name this is my script.
Code:
touch ${fnom}.{1..20}
mv ${fnom} ${fnom.1}

mv ${fnom}.20 ${fnom}.21
mv ${fnom}.19 ${fnom}.20
mv ${fnom}.18 ${fnom}.19
mv ${fnom}.17 ${fnom}.18
mv ${fnom}.16 ${fnom}.17
mv ${fnom}.15 ${fnom}.16
mv ${fnom}.14 ${fnom}.15
mv ${fnom}.13 ${fnom}.14
mv ${fnom}.12 ${fnom}.13
mv ${fnom}.11 ${fnom}.12
mv ${fnom}.10 ${fnom}.11
mv ${fnom}.9 ${fnom}.10
mv ${fnom}.8 ${fnom}.9
mv ${fnom}.7 ${fnom}.8
mv ${fnom}.6 ${fnom}.7
mv ${fnom}.5 ${fnom}.6
mv ${fnom}.4 ${fnom}.5
mv ${fnom}.3 ${fnom}.4
mv ${fnom}.2 ${fnom}.3
mv ${fnom}.1 ${fnom}.2

echo "COMPLETE"

here is the error I am getting

Code:
 line 28: ${fnom.1}: bad substitution
mv: cannot stat `foo.1': No such file or directory

If the dir only had a file named foo ... then after the script runs foo.(3-20) exist but foo.1 and foo.2 do not.

Last edited by Scott; 04-27-2010 at 06:38 PM.. Reason: Code tags, please...
# 5  
Old 04-27-2010
You cannot use ${fnom.1}. You need to use ${fnom}.1

Use curly braces only around the variable name.
# 6  
Old 04-27-2010
Thanks for catching that mistake. Now I'm trying to delete any files that are greater than the number input.

I want to write something like
numberinput+1

rm ${name}.${numberinput+1..99}
# 7  
Old 04-27-2010
Use a for loop:

Code:
for i in {1..10}
do
  echo "$i"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help on simple script as i dont know numch about UNIX scripting

Hello All, My name is vasu and I am very new to Unix scripting, i know basic commands, but now i need to write the following script, i have tried but no luck My requirment is i am getting one our from another command as following Used:1.8TB Advisory Quota:1.8TB aaa1 Used:4.5TB Advisory... (1 Reply)
Discussion started by: VasuKukkapalli
1 Replies

2. Shell Programming and Scripting

Help/How-to - simple UNIX script / gzip (beginner)

Hey all, I would like to ask your help and sorry in advance for my ignorance since I am a complete beginner in this. I need to create a UNIX script that will: - scan a small number of folders and subfolders (see a similar file tree in the attachment) - - for each final folder (each of... (8 Replies)
Discussion started by: McNulty
8 Replies

3. Shell Programming and Scripting

need a script that does a simple task on multiple unix servers.

hi guys, i need a script that does a simple task on multiple aix servers. if possible with both telnet and ssh. the simple task i wanna do is connect to a server and run "ifconfig -a" and get the output. nextweek i need to do similar jobs on like 50 servers... :( can anybody help me with making... (2 Replies)
Discussion started by: curtis911
2 Replies

4. UNIX for Dummies Questions & Answers

Define variables with UNIX script

oopps! I Meant "Define Variables within a UNIX Script" What would be the best way to define a variable in a unix shell script so anyone who views this script doesn't know what value is assigned to that variable. some other location... a="/usr/lib/fileA" Unix script... sed... (5 Replies)
Discussion started by: macastor
5 Replies

5. Shell Programming and Scripting

Cannot execute Unix command in a simple perl script

Am trying to lean perl scripting in Unix OS to automate my tasks. Please find the below perl script i have tried #!/usr/bin/perl -w print "Please Enter the VG name to be checked:"; $A = <>; print "Please Enter the free size to be checked in GB:"; $B = <>; $vgcheck = `vgdisplay... (7 Replies)
Discussion started by: jayachandran87
7 Replies

6. Shell Programming and Scripting

Adding variables in a unix script

Hi I am trying to add variables(float values) in a unix script but am getting an error value=`expr $a + $b + $c` The error I am getting is "expr: non-numeric argument" I guess it has got to something with the decimal points. Plz help (13 Replies)
Discussion started by: akashtcs
13 Replies

7. Shell Programming and Scripting

Simple Script looking for Hard Coded Variables

Hi Guys Looking for a little help with a script to grep all files looking for hard coded variables - ie its IP address... so we know which files to look at before an IP change... This is what I have - but it seems to loop and never end... Any better suggestions? #!/usr/bin/ksh #simple... (2 Replies)
Discussion started by: serm
2 Replies

8. Shell Programming and Scripting

A simple query on unix shell script

I want to write a script to go to particular path in file and run shell script from there. what will be shell script for the same. (2 Replies)
Discussion started by: shekhar_ssm
2 Replies

9. Shell Programming and Scripting

simple unix script help

when i run this script: # Author: xtos if then echo "Please enter ONLY one name to search, (example "$0" NAME)" exit fi INPUT="grep $1 $HOME/work/phone_book" if then echo "Listing not found, please try again." else (2 Replies)
Discussion started by: xtos
2 Replies

10. UNIX for Dummies Questions & Answers

Simple UNIX Shell Script help, PLEASE

I don't know anything about UNIX. I have been developing on NT platform and now a batch file I was running in my java code must work on UNIX instead. How do I change the below .bat file into a shell script that can be run on UNIX? Thanks in advance for your help. @ECHO OFF D:... (1 Reply)
Discussion started by: ci2a020
1 Replies
Login or Register to Ask a Question