Visit Our UNIX and Linux User Community

Script template for inputting filenames and print results

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script template for inputting filenames and print results
# 1  
Old 02-24-2011
Script template for inputting filenames and print results


Hope you are all well. New to scripting, and all those characters are all a new language for me. Though hoping to get my little head round it all sooner or later.

I was wondering whether anyone could help with a script template example.

What I would like to happen is to run the script and for it to respond with whatever file I would like the script to check and then perform an md5sum check on it and then print out the results.

The thing is, that all hosts are on the same LAN but requires ssh into it.

Would appreciate any help given so I can start to get my head round this scripting marlakey.

Thanks in advance.

---------- Post updated at 11:05 AM ---------- Previous update was at 11:03 AM ----------

Oh and it's Linux RH5 if that helps :-)
# 2  
Old 02-24-2011

(( $# < 1 )) && {
  printf "usage: %s <file_name>\n" "${0##*/}"
  exit 1



for s in "${_server_list[@]}"; do
  ssh "$s" "
    [ -f "$_fn" ] && 
      $_cmd $_fn" ||
        printf "error executing %s %s on %s\n" \
          "$_cmd" "$_fn" "$s"

# 3  
Old 02-24-2011
Some of the double quotes might need escaping in the ssh command above.

Here is a version with a simple menu which you might find usefull examples of:
  • Select statement with validation and additional quit option
  • $(( )) calculations
  • Fetch number of elements in an array using ${#array[@]}
  • If then else clause

(( $# < 1 )) && {
    printf "usage: %s <file_name>\n" "${0##*/}"
    exit 1
_server_list=( server1 server2 )
select s in "${_server_list[@]}" "quit"; do
    if [[ -z "$s" ]]
        echo "Option $REPLY is invalid - Please select a number from 1 to $(( ${#_server_list[@]} + 1))"
        [[ "$s" = "quit" ]] && break
        ssh "$s" "
            [ -f \"$_fn\" ] && 
                $_cmd $_fn ||
                printf \"error executing %s %s on %s\n\" \\
\"$_cmd\" \"$_fn\" \"$s\""

Last edited by Chubler_XL; 02-24-2011 at 06:56 PM.. Reason: Cleanup formatting
# 4  
Old 02-25-2011
Originally Posted by Chubler_XL
Some of the double quotes might need escaping in the ssh command above.
Yes, thanks!
Actually, more quotes are needed:

_cmd="[ -f '$_fn' ] && /usr/bin/md5sum '$_fn'"

for s in "${_server_list[@]}"; do
  ssh "$s" "$_cmd" ||
        printf  >&2 "error executing %s %s on %s\n" \
                  "$_cmd" "$_fn" "$s"

Last edited by radoulov; 02-25-2011 at 05:51 AM..
# 5  
Old 02-25-2011
My god, yep it does look all weird to me :-)

Thanks for the input guys, really appreciate it. I will try this out on my test system and let you know the outcome. Hope it all goes well.

Again, thank you.

---------- Post updated at 04:37 AM ---------- Previous update was at 03:41 AM ----------

WOW, thanks, just tested it and it's just what I needed.

I maybe pushing the boat out a little bit further, what should I put in the file if I wanted an option to use all the servers in the server list as well as currently inputting one server at a time.

Thank you though guys. You really helped me out here.
# 6  
Old 02-25-2011
Assuming the server list is a file in the following format:


You could use something like this:


while IFS= read -r; do
  ssh "$REPLY" "$_cmd" ||
        printf "error executing %s %s on %s\n" \
                  "$_cmd" "$_fn" "$REPLY"
done < "$_slist"

Or, of course, just place all your servers in the _server_list array variable.
# 7  
Old 02-25-2011
Hi guys,

I've noticed that the output is shown twice and a message is given, is there a reason why that is:

#? 1
96fbd1ef56e61e8455f356614516b829 /opt/xxx/lib/
96fbd1ef56e61e8455f356614516b829 /opt/xxx/lib/
/usr/bin/md5sum: server02: No such file or directory

Previous Thread | Next Thread
Test Your Knowledge in Computers #71
Difficulty: Easy
Linux, macOS, Android, iOS, Chrome OS, Orbis OS used on the PlayStation 4, whatever firmware is running on your router, all of these operating systems are generally referred to as a Unix-like operating system.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

print all filenames in directory with path to another file

hi, i have a directory at /path/unix with the following files 1.txt 2.txt 3.txt 4.txt I want to make another file called filenames.txt at a different location called /path/home. So, my output file would be /path/home/filenames.txt with contents /path/unix/1.txt... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

2. Shell Programming and Scripting

how to print off results

print from an ip_list file containing 300 ip's the directory of the results is /var/tmp/ the will change according to the /tmp/ip_list file i.e I need the results from /var/tmp/ once done the script goes to the next ip address in... (11 Replies)
Discussion started by: slashbash
11 Replies

3. Shell Programming and Scripting

Print and Append the matching filenames

Hi, I want to check all files in a folder for some specific start string and append all matching filenames with _1, _2 .... for each file found in the directory. But, $file below gives me all details of the files like access permissions, owner and filename etc. I just want all the filenames... (3 Replies)
Discussion started by: chetancrsp18
3 Replies

4. Shell Programming and Scripting

[awk] print all filenames in directory

Hello, I was given a (I suppose) a simple task which I don't know how to do. Before coming here I have read a dozen of awk tutorials (full read, not just line-skipping) but I still don't know how to do this. Task: Write an script file 'check.awk' with a parameter current directory that... (5 Replies)
Discussion started by: c0dehunter
5 Replies

5. Shell Programming and Scripting

Print some results in a text file using script in linux

hello everyone, i really need your help to write a script which would just print following kind of result into a text file (result.txt) XYZ test Results ID: <unique-id> Date: <date> ------------------------------------------------- | Task | Result | Time |... (3 Replies)
Discussion started by: viriimind
3 Replies

6. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

7. Shell Programming and Scripting

How to print the last column of the list contain filenames with spaces.

Hi experts, I have the following data: I want the last filed in the output. How to print the last field , It contains the file names and few filenames with white spaces . -rw-r--r-- 1 root root 0 2010-04-26 16:57 file1 2space_File.txt -rw-r--r-- 1 root root 0 2010-04-26... (2 Replies)
Discussion started by: rveri
2 Replies

8. Shell Programming and Scripting

Print just 1 results from the whole line

Hi All, need some help with writing a shell script. I tried to search this forum but couldn't find exactly what i want. If you all know any reference link i can read and refer to solve my issue, let me know. I got 1 file i.e: example.txt. It is content list of data, as below example. What... (2 Replies)
Discussion started by: anakiar
2 Replies

9. Shell Programming and Scripting

Print filenames with spaces using awk

Hello all, I want to list the file contents of the directory and number them. I am using la and awk to do it now, #ls |awk '{print NR "." $1}' 1. alpha 2. beta 3. gamma The problem I have is that some files might also have some spaces in the filenames. #ls alpha beta gamma ... (7 Replies)
Discussion started by: grajp002
7 Replies

10. Shell Programming and Scripting

search file and print results with shell script

Discussion started by: dodasajan
3 Replies

Featured Tech Videos