Debian Codegraph @

sedappend.sh

raw symbols SCIP
1 #!/bin/bash
2 # Prepends a string at a specified line
3 #+ in files with names ending in "sample"
4 #+ in the current working directory.
5 # 000000000000000000000000000000000000
6 # This script overwrites files!
7 # Be careful running it in a directory
8 #+ where you have important files!!!
9 # 000000000000000000000000000000000000
10
11 # Create a couple of files to operate on ...
12 # 01sample
13 # 02sample
14 # ... etc.
15 # These files must not be empty, else the prepend will not work.
16
17 lineno=1 # Append at line 1 (prepend).
18 filespec="*sample" # Filename pattern to operate on.
19
20 string=$(whoami) # Will set your username as string to insert.
21 # It could just as easily be any other string.
22
23 for file in $filespec # Specify which files to alter.
24 do # ^^^^^^^^^
25 sed -i ""$lineno"i "$string"" $file
26 # ^^ -i option edits files in-place.
27 # ^ Insert (i) command.
28 echo ""$file" altered!"
29 done
30
31 echo "Warning: files possibly clobbered!"
32
33 exit 0
34
35 # Exercise:
36 # Add error checking to this script.
37 # It needs it badly.